I want to write some JavaScript that will change the onmousedown of a div at runtime. So on load a mouse down on the div will do one thing and if a JavaScript function is called a mouse down on the div will do something else. Is this possible?
I want to write some JavaScript that will change the onmousedown of a div at runtime. So on load a mouse down on the div will do one thing and if a JavaScript function is called a mouse down on the div will do something else. Is this possible?
Share Improve this question edited Dec 6, 2013 at 14:50 BenMorel 36.6k51 gold badges205 silver badges335 bronze badges asked Oct 5, 2008 at 0:36 mrjrdnthmsmrjrdnthms 1,6294 gold badges25 silver badges35 bronze badges 1- @ mrjrdnthms : What Prestaul is attaching to the handler is the definition of a new function. What you are attaching is the return of the alert function, that is void/undefined, IIRC. This is why is code works, and your won't. – paercebal Commented Oct 5, 2008 at 12:04
1 Answer
Reset to default 13You can just assign the onMouseDown property.
document.getElementById('myDiv').onmousedown = function() {
alert('New mouse down handler.');
};
Keep in mind that javascript properties are case-sensitive. "onmousedown" is all lower-case.