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
JavaScript
x
19
19
1
<div class="setup" onclick="show(1)">
2
Click to show
3
<p class="hidden">
4
I'm more stuff
5
</p>
6
</div>
7
8
9
function show(elem) {
10
var p = document.getElementsByClassName("hidden");
11
if (p[elem] != undefined) {
12
if (p[elem].style.display == "none") {
13
p[elem].style.display = "block";
14
} else {
15
p[elem].style.display = "none";
16
}
17
}
18
}
19
http://jsfiddle.net/ba7yfmz6/29/
Advertisement
Answer
Use this
:
JavaScript
1
2
1
<div class="setup" onclick="show(this)">
2
JavaScript:
JavaScript
1
8
1
function show(elem) {
2
var paragraph = elem.querySelector(".hidden");
3
if (paragraph.style.display == "none") {
4
paragraph.style.display = "block";
5
} else {
6
paragraph.style.display = "none";
7
}
8
Hopefully this helps!