onclick function() doesn't work... | Sololearn: Learn to code for FREE!


onclick function() doesn't work...

in the html i code: <h1 id="h1">Hello</h1> <button id="bt1" onclick="click()">A</button> in the js page, i code: function click(){ document.getElementById("h1").innerHTML="World"; } when i click the button, nothing happens... any idea why?

3/11/2020 6:35:43 PM

Sharon Keren

3 Answers

New Answer


Modify your JavaScript code: window.onload = () => { // rewrite your code here }


//Actually onclick event is working but function you declared "click" DOM elements have a "click()" method on Which stimulates a mouse click , so making it function namw won't work //Abou click method https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click


change id name to bt1 in js code