Here is my code :
It is simple, but I can't get it to work.
When I copied the code inside and ran it at console, it worked perfectly.
Answer
The onclick event isn't firing because the element doesn't exist at the time you bind the click event (DOM isn't loaded yet).
To fix this you can put the script before you close the body tag (1)
or wrap your code in a DOM loaded event (2).
1)
2)
window.onload = function () {
document.getElementById("id").onclick = function(){
alert();
}
}
No comments:
Post a Comment