+ 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 Respostas
+ 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