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.
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.
– Tiu Hiikou
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?
– dfvc
I will edit the post so that it is easier to understand.
– Tiu Hiikou
I updated the answer. This is what you want?
– dfvc
Almost that, but your answer gave me a boom of what needed to be done.
– Tiu Hiikou