HELP! How do you call a javascript function with the HTML onclick keyword? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

HELP! How do you call a javascript function with the HTML onclick keyword?

My code looks like this in HTML: <div class=“answer11” onclick=“extrovert1()”>Button</div> And my javascript code: function extrovert1() { x—; document.getElementbyClassName(“answer11”).style.backgroundColor = “green”; As you can see I’m trying to change the background color to green when the HTML element is clicked. However I keep getting the error, document.getElementByClassName is not a function at extrovert1 at HTMLDivElement.onclick. Why isn’t the onclick keyword calling the function?

20th Jul 2020, 8:16 PM
tori the crafter 💛
tori the crafter 💛 - avatar
3 Answers
+ 2
Also getElementsByClassName returns an array like object(nodelist) ,so not all array operations can be performed on it
20th Jul 2020, 8:36 PM
Abhay
Abhay - avatar
+ 1
Ohh i feel so dumb rn thanks for the clarification! Bagon Abhay
20th Jul 2020, 8:40 PM
tori the crafter 💛
tori the crafter 💛 - avatar
+ 1
it will be document.getElementsByClassName("answer11") ; you missed s after Element. for accessing using class and tag we use Elements for accessing using id we use Element
21st Jul 2020, 9:39 AM
Divya Mohan
Divya Mohan - avatar