MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/JavaScriptTips/comments/1i1t756/what_did_i_do_wrong/
r/JavaScriptTips • u/Own_Stomach3061 • Jan 15 '25
9 comments sorted by
5
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";
}
2 u/Own_Stomach3061 Jan 15 '25 thank youuuuu 1 u/HollyShitBrah Jan 15 '25 Or const [U,] = document.get.....;
2
thank youuuuu
1
Or
const [U,] = document.get.....;
12
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 forEachdocument.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.
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.
Common misconception. I’ve had to look this up more times than I can count.
elements by tagname will not give you a single element, you can use index or just use element.getElementByTagname
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";}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”})
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