Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  15] [ 6]  / answers: 1 / hits: 15384  / 6 Years ago, wed, december 12, 2018, 12:00:00

Is there a more dynamic way to hide/show divs that are identical in structure with no identifiers?

Click to show

I'm some stuff




<div class=setup onclick=show(1)>
Click to show
<p class=hidden>
I'm more stuff
</p>
</div>


function show(elem) {
var p = document.getElementsByClassName(hidden);
if (p[elem] != undefined) {
if (p[elem].style.display == none) {
p[elem].style.display = block;
} else {
p[elem].style.display = none;
}
}
}


http://jsfiddle.net/ba7yfmz6/29/


More From » html

 Answers
49

Use this:



<div class=setup onclick=show(this)>


JavaScript:



function show(elem) {
var paragraph = elem.querySelector(.hidden);
if (paragraph.style.display == none) {
paragraph.style.display = block;
} else {
paragraph.style.display = none;
}


Hopefully this helps!


[#52934] Thursday, December 6, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aleenamarinr

Total Points: 610
Total Questions: 109
Total Answers: 118

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
;