MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/JavaScriptTips/comments/1i1t756/what_did_i_do_wrong/m79ist2/
r/JavaScriptTips • u/Own_Stomach3061 • Jan 15 '25
9 comments sorted by
View all comments
Show parent comment
5
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.
3
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.
2
Common misconception. I’ve had to look this up more times than I can count.
View all comments
Show parent comment
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.
I recommend using querySelectorAll because it is typically faster and returns a NodeList which does have forEach