Why is JS getElementsByClassName method not working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is JS getElementsByClassName method not working?

Hi all, I have an issue here. I'm totally new to javascript and I just completed the course here, so i have been playing around with the codes. I came to realize that document.getElementsByClassName is not working at all or is it? Where could I be wrong? or is there another alternative to this?

29th Sep 2018, 9:37 PM
Dzeff
Dzeff - avatar
2 Answers
+ 8
Working fine for me remember that document.getElementsByClassName returns a collection of elements and not just a single element like document.getElementById so you have to either iterate on the elements (as i did in the code below, changeAll() function) OR choose specificly the one you want by its index (also in the code below, changeMiddle() function): var boxes = document.getElementsByClassName("boxes"); var myBox = boxes[1]; https://code.sololearn.com/WHYS5Za1jiJ1/?ref=app
29th Sep 2018, 10:09 PM
Burey
Burey - avatar
0
thanks Burey
30th Sep 2018, 2:51 AM
Dzeff
Dzeff - avatar