0
How can I hide all the DIVs whose IDs are in myArray?
I have this on my HTML page: <div id="D1"></div> <div id="D2"></div> <div id="D3"></div> I have a variable in Javascript like this: var myArray = ["D1", "D3"]; I know I can hide a div using the DOM document.getElementById().style.display = “none”, but it only hides the one div with that specific ID. How can I hide all the DIVs whose IDs are in myArray? Thanks in advance!
3 Antworten
+ 6
document.querySelectorAll('#'+myArray.join(', #')).forEach(e => e.style.display = "none");
+ 1
I guess it's because I'm new, but all that seem very foreign to me..
I did eventually figure it out:
var myArray = ["D1", "D3"];
for (i = 0; i < myArray.length; i++) {
document.getElementById(myArray[i]).style.display = "none";
}
It just didn't work previously because I declared the myArray variable outside of the function. Thanks guys!
0
Using myArray.forEach( myArrayparam =>{
myArrayparam.style...
});