Access of multiple ID`s and hiding them (in CSS) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Access of multiple ID`s and hiding them (in CSS)

Ok so I have a strange problem. I need to hide (with a button) multiple elements on site (and later turn them visible). Now I`ve tried few different approaches. 1. Adding common CLASS to elements, then in JS function I access them by document.getElementsByClassName(); -Didn`t worked though. 2. Because elements are of the same type (tag), i`ve tried getElementsByTagName(); - Also recieved error. 3. By using document.querySelectorAll(); list of ID`s... - Didn`t worked also. 4. By creating an array of ID`s (var a = [document.getElementById()...]; None if this actually worked. I am constantly recieving error "JavaScript Console TypeError: dis.style is undefined" My piece of code: function clockOn() { var dis = [ document.getElementById("ora"), document.getElementById("minu"), document.getElementById("seco"), document.getElementById("merry"), document.getElementById("wDay"), document.getElementById("calDay"), document.getElementById("year") ]; if (dis.style.visibility === 'hidden') { dis.style.visibility = 'visible'; } else { dis.style.visibility = 'hidden'; } } I know it would be easier to use JQ but this is not the point. Maybe I am missing something? I am really confused at this moment. Thanks for help. Everything is here. https://code.sololearn.com/WoRys0ur8xfq/#html

8th Apr 2018, 5:53 PM
Karol Michalik
Karol Michalik - avatar
2 Answers
+ 2
You need to loop your array for(var a = 0; a < dis.length; a++) { if (dis[a].style.visibility === 'hidden') { dis[a].style.visibility = 'visible'; } else { dis[a].style.visibility = 'hidden'; } }
8th Apr 2018, 6:02 PM
Toni Isotalo
Toni Isotalo - avatar
0
Brilliant... Yes it works! (it lives!) " thank you so much!
9th Apr 2018, 12:25 PM
Karol Michalik
Karol Michalik - avatar