"Why isn't get element by class working in my code?"
Hey guys,
So I’m trying to use get element by class to grab some divs, but it’s just not working??
I’ve got `document.getElementsByClassName('my-class')`, but when I log it, it’s empty. The elements are definitely there in the HTML tho.
Am I missing something? Like, does the DOM need to load first or something?
Also, I noticed get element by class returns an HTMLCollection, not an array. Do I need to convert it or something to loop through?
Any help would be awesome, thanks!
(ps. using vanilla JS, no jQuery)
---
OR
---
"Can someone explain get element by class with multiple elements?"
Yo,
Quick question—how do you handle multiple elements with get element by class?
I know it grabs all matches, but how do you actually *use* them? Like, if I wanna add a click event to each one, do I have to loop through the collection?
Tried `forEach` but got an error lol. Turns out get element by class gives an HTMLCollection, not an array.
Sooo… `Array.from()` or spread operator? What’s the cleanest way?
Also, is there a better method than get element by class for dynamic content?
Thanks in advance!
Hey guys,
So I’m trying to use get element by class to grab some divs, but it’s just not working??
I’ve got `document.getElementsByClassName('my-class')`, but when I log it, it’s empty. The elements are definitely there in the HTML tho.
Am I missing something? Like, does the DOM need to load first or something?
Also, I noticed get element by class returns an HTMLCollection, not an array. Do I need to convert it or something to loop through?
Any help would be awesome, thanks!
(ps. using vanilla JS, no jQuery)
---
OR
---
"Can someone explain get element by class with multiple elements?"
Yo,
Quick question—how do you handle multiple elements with get element by class?
I know it grabs all matches, but how do you actually *use* them? Like, if I wanna add a click event to each one, do I have to loop through the collection?
Tried `forEach` but got an error lol. Turns out get element by class gives an HTMLCollection, not an array.
Sooo… `Array.from()` or spread operator? What’s the cleanest way?
Also, is there a better method than get element by class for dynamic content?
Thanks in advance!
