onclick function() doesn't work... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

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?

11th Mar 2020, 6:35 PM
Sharon Keren
Sharon Keren - avatar
3 Answers
+ 6
Modify your JavaScript code: window.onload = () => { // rewrite your code here }
11th Mar 2020, 6:41 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 6
//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
11th Mar 2020, 6:50 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 1
change id name to bt1 in js code
14th Mar 2020, 3:13 PM
Shadman Sakib
Shadman Sakib - avatar