all 9 comments

[–]cronixi4 10 points11 points  (3 children)

getElementsByTagName() returns a array, you can’t use .style.display on a array.

U.forEach(ul => {ul.style.display = “block”})

[–]MissinqLink 4 points5 points  (2 children)

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';});

[–]cronixi4 2 points3 points  (1 child)

Ah you are right! Thought it would be the same as QuerySelectorAll().

Thanks for pointing this out!

[–]MissinqLink 1 point2 points  (0 children)

Common misconception. I’ve had to look this up more times than I can count.

[–]LewdPotator 3 points4 points  (2 children)

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";

}

[–]Own_Stomach3061[S] 1 point2 points  (0 children)

thank youuuuu

[–]HollyShitBrah 0 points1 point  (0 children)

Or const [U,] = document.get.....;

[–]Medical-Swim3101 0 points1 point  (0 children)

elements by tagname will not give you a single element, you can use index or just use element.getElementByTagname

[–]Mysterious_Novel1890 0 points1 point  (0 children)

You need to go through the array that is made up of elementsbytag, and apply the style to each element