Problem with "see more" (button)!

Asked

Viewed 52 times

0

I am with a small project that is to optimize the "see more" on a site or application and I came across a problem.

<div class="conteudo">
  <h2>
    <span>teste</span>
  </h2>
  <p>
   Lorem ipsum dolor sit amet, consectetur adipiscing elit
. Donec tincidunt quis nibh eget feugiat. 
  </p>
  <hr />
</div>

<div class="conteudo">
  <h2>
    <span>teste2</span>
  </h2>
  <p>
   Lorem ipsum dolor sit amet, consectetur adipiscing elit
. Donec tincidunt quis nibh eget feugiat. 
  </p>
  <hr />
</div>

<div class="conteudo">
  <h2>
    <span>teste3</span>
  </h2>
  <p>
   Lorem ipsum dolor sit amet, consectetur adipiscing elit
. Donec tincidunt quis nibh eget feugiat. 
  </p>
  <hr />
</div>

The idea is that every time you click on what is written in "span", I can see the text and when you click on it again, I can hide the text.

Making a function for this is really easy actually, by changing the value of T, you can make the buttons work.

t = 0;
var span = document.querySelectorAll(".conteudo span")[t];

span1.addEventListener("click", function () {
  var cont = document.querySelectorAll(".conteudo p")[t];

  if (cont.classList.contains("mostrar")) {
    cont.classList.remove("mostrar");
  } else {
    cont1.classList.add("mostrar");
  }
});

With this, you solve the problem, however, from here it gets "complicated". To "solve" this problem, you would have to copy the code in JS several times and change the value of T. I was trying to do it in a way that I didn’t need to duplicate the code and change its value.

So I turn to you, fellow programmers to help me in this wonderful infernal endeavor.

I apologize in advance, I am beginner in programming and put on the forum for the first time.

2 answers

0

One option would be to use the target click to identify the clicked element and then add/remove (using the classList.toggle) the class mostrar.

See the following example:

document.addEventListener('click', (e) => {
  if (e.target.tagName.toLowerCase() === 'h2' && e.target.parentNode.classList.contains('conteudo')) {
    e.target.nextElementSibling.classList.toggle('mostrar');
  }
});
p {
  display: none;
}

p.mostrar {
  display: block;
}
<div class="conteudo">
  <h2>Teste 1</h2>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tincidunt quis nibh eget feugiat.
  </p>
</div>

<div class="conteudo">
  <h2>Teste 2</h2>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tincidunt quis nibh eget feugiat.
  </p>
</div>

<div class="conteudo">
  <h2>Teste 3</h2>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tincidunt quis nibh eget feugiat.
  </p>
</div>

  • No doubt, but I would still have the problem that there would be other classes with the same problem of clicking on it, the content does not appear, only in the first selected.

  • 1

    I confess that I find it very difficult to understand what you want. Already several times, either your question or the comment, and it is still unclear what you want to do. Can specify a little more?

  • I will edit the post so that it is easier to understand.

  • I updated the answer. This is what you want?

  • Almost that, but your answer gave me a boom of what needed to be done.

0

var span = document.querySelectorAll(".conteudo");

I managed to solve the problem, I ended up having to use for each.

span.forEach((item) => {
  item.addEventListener("click", function () {
    const phr = this.querySelector("p");
    if (phr.classList.contains("mostrar")) {
      phr.classList.remove("mostrar");
    } else {
      phr.classList.add("mostrar");
    }
  });
});
  • This way the class is added/removed whenever you click on any zone of a div content, that is, when the paragraph is visible and clicked, it will also hide. Your question implies that the paragraph should be shown/hidden only when you click on H2. Just one more suggestion ... can easily replace if/Else using classList.toggle, as in the code I wrote earlier.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.