Follow up to my previous question - I want to use a button to show / hide multiple elements with the same class name using JS, yet it appears that I can only change the first element with a certain class name, and all further elements with the same class on the page are ignored.
How do I fix this?
function designInfo() {
document.getElementsByClassName("design")[0].style.display = "block";
document.getElementsByClassName("it")[0].style.display = "none";
document.getElementsByClassName("other")[0].style.display = "none";
}
function itInfo() {
document.getElementsByClassName("design")[0].style.display = "none";
document.getElementsByClassName("it")[0].style.display = "block";
document.getElementsByClassName("other")[0].style.display = "none";
}
function allInfo() {
document.getElementsByClassName("design")[0].style.display = "block";
document.getElementsByClassName("it")[0].style.display = "block";
document.getElementsByClassName("other")[0].style.display = "block";
}
.it {}
.design {}
.other {}
.indent {
margin: .5em 1em .5em 1em;
}
<button onclick="designInfo()">show design stuff</button>
<button onclick="itInfo()">show IT stuff</button>
<button onclick="allInfo()">show all</button>
<div class="indent">
<div class="it">• boring IT stuff</div>
<div class="design">• cool design stuff</div>
<div class="it">• it stuff and things</div>
<div class="design">• design stuff</div>
<div class="it">• it stuff and more</div>
<div class="other">• more it stuff</div>
<div class="other">• it stuff</div>
</div>
[0]after your selection will get the first node and only the first node.document.getElementsByClassName.