Why getElementsByClassName doesnt work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why getElementsByClassName doesnt work?

wanna use innerHTML at button2 https://code.sololearn.com/W6MfGH0xPpuF/?ref=app https://code.sololearn.com/W6MfGH0xPpuF/?ref=app

11th Dec 2017, 8:14 AM
Sergey
2 Answers
+ 8
Add [0] after document.getElementsByClassName("bUt") do like this:- var z = document.getElementsByClassName("bUt")[0]; This is because when you use the 'getElementsByClassName' it returns an array..So we have to get the element by using the index, here it is the first element so we say [0].. Remember index starts from 0 not 1.. Alternative way, as z is the collection of array so we can also do this :- z[0].innerHTML But if you want a separate variable for the button use the first way
11th Dec 2017, 10:54 AM
Nikhil
Nikhil - avatar
+ 4
Change z.innerHTML to z[0].innerHTML
11th Dec 2017, 10:55 AM
Md. Nafis Ul Haque Shifat
Md. Nafis Ul Haque Shifat - avatar