+ 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.
6 Réponses
+ 6
getElementsByClassName returns an array of elements
+ 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. :(
+ 8
Dark Angel No confusion, I get it from your descriptive information. :)
+ 6
Ooh, sorry bout that. Thanks!
+ 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(...)
+ 4
If you want to avoid the confusion you can use an id instead of a class.