Thursday 15 January 2015

Javascript onclick event not working -



Javascript onclick event not working -

this question has reply here:

why jquery or dom method such getelementbyid not find element? 6 answers

on running below code, getting next error - "uncaught typeerror: cannot set property 'onclick' of null"

<!doctype html> <html> <head> <script> document.getelementbyid("x").onclick = function() { alert("clicked button"); }; </script> </head> <body> <button id="x">this button</button> </body> </html>

you cannot element id because document not loaded.

try add together onload function:

window.onload = function() { document.getelementbyid("x").onclick = function() { alert("clicked button"); }; };

javascript events

No comments:

Post a Comment