I’m looking for a way I can hide each <p></p>
and show <h1></h1>
instead on click of a button (onclick="javascript hide <p> show <h1>
), and vise versa. I could do it with unique id but I cannot assign the same id for each. Is there any way to do it? preferably without jquery.
<div class="Title">
<div class="SubTitle">
<p>Text</p>
<h1>Text2</h1>
</div>
</div>
<div class="Title">
<div class="SubTitle">
<p>Text</p>
<h1>Text2</h1>
</div>
</div>
<div class="Title">
<div class="SubTitle">
<p>Text</p>
<h1>Text2</h1>
</div>
</div>
2
Answers
You can use the css selector
>
for direct child of, and say that the direct childp
elements ofdiv
elements are all toggled the hidden class when the button is clicked. You could target more specifically withdiv > div > p
or.SubTitle > p
as well.Sure.
Here I add an event listener to a button.
I hid each p first, since I assume you want to show the title when the page loads.
Alternatively hide the Ps onload using script when loading the page:
Note: that will show the p’s while the page loads, but then again that also shows the user that there is data under each title