12
u/cronixi4 Jan 15 '25 edited Jan 15 '25
getElementsByTagName() returns a array, you can’t use .style.display on a array.
U.forEach(ul => {ul.style.display = “block”})
5
u/MissinqLink Jan 15 '25 edited Jan 15 '25
It returns an HTMLCollection which doesn’t have array methods. If you spread to an array it will work.
[...U].forEach(x=>{x.style.display = 'block';});I recommend using querySelectorAll because it is typically faster and returns a NodeList which does have forEach
document.querySelectorAll('ul').forEach(x=>{x.style.display = 'block';});3
u/cronixi4 Jan 15 '25
Ah you are right! Thought it would be the same as QuerySelectorAll().
Thanks for pointing this out!
2
u/MissinqLink Jan 15 '25
Common misconception. I’ve had to look this up more times than I can count.
1
u/Medical-Swim3101 Jan 15 '25
elements by tagname will not give you a single element, you can use index or just use element.getElementByTagname
1
u/Mysterious_Novel1890 Jan 16 '25
You need to go through the array that is made up of elementsbytag, and apply the style to each element


5
u/LewdPotator Jan 15 '25
If I'm not wrong you can't change all the tags directly, so either use index or loop the tag names.
U[0].style.display = "block";or
for (let i = 0; i < U.length; i++) {U[i].style.display = "block";}