Onclick Event - Why Javascript Runs It Onload
some content
I'm just starting out with Javascript, aSolution 1:
This is because you didnt wrap the onclick
assignment as an actual function, so it attempts to assign the result of alert('clicked')
to the onclick event handler (which means it's probably undefined
when assigned). What you need to do is assign a function to that handler like so:
document.getElementById('specialp').onclick = function()
{
alert('clicked');
};
When you do the same thing in HTML, the DOM automatically wraps that content in a function for you.
Post a Comment for "Onclick Event - Why Javascript Runs It Onload"