+ 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?
5 Answers
+ 2
Also getElementsByClassName returns an array like object(nodelist) ,so not all array operations can be performed on it
+ 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