[FIXED] JavaScript - How can I fix this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

[FIXED] JavaScript - How can I fix this?

I tried everything I can to fix this error, I also followed the syntax given in JS lessons. But sill got an error and I have no idea why. Just open and run this code to understand my situation.

20th Sep 2018, 10:57 PM
Email Not Activated
6 Answers
+ 6
getElementsByClassName returns an array of elements
20th Sep 2018, 11:06 PM
Toni Isotalo
Toni Isotalo - avatar
+ 8
Thanks Toni Isotalo Dark Angel! Now I understand it. It's really far from jQuery (where I'm used to write it). And I forgot the pure JS. :(
20th Sep 2018, 11:12 PM
Email Not Activated
+ 8
Dark Angel No confusion, I get it from your descriptive information. :)
20th Sep 2018, 11:15 PM
Email Not Activated
+ 6
Ooh, sorry bout that. Thanks!
20th Sep 2018, 11:07 PM
Email Not Activated
+ 5
document.getElementsByClassName() returns an array of elements with that class name, even if it is 1 or 100. Hence, an array object cannot use the appendChild method. To fix this, replace dropdown.appendChild(...) with dropdown[0].appendChild(...)
20th Sep 2018, 11:09 PM
๐Ÿ‘‘ Prometheus ๐Ÿ‡ธ๐Ÿ‡ฌ
๐Ÿ‘‘ Prometheus ๐Ÿ‡ธ๐Ÿ‡ฌ - avatar
+ 4
If you want to avoid the confusion you can use an id instead of a class.
20th Sep 2018, 11:13 PM
๐Ÿ‘‘ Prometheus ๐Ÿ‡ธ๐Ÿ‡ฌ
๐Ÿ‘‘ Prometheus ๐Ÿ‡ธ๐Ÿ‡ฌ - avatar