0
Why it isn't work?
Why it isn't work? I want to changing properties with java script but i got error https://code.sololearn.com/WMw5w08zPAL4/?ref=app
3 Answers
+ 2
getElementsByClassName doesn't return an array btw.
+ 1
"The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s)."
~MDN
The returned HTMLCollection doesn't have a style property.
As if you want to access nth element you can access it using index n -1
To access first matching element use index 0
//Correct code :
document
.getElementsByClassName("test")[0]
.style.backgroundColor = "blue" ;
See how [0] used after ("test")
https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName