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 fuction is called a mouse down on the div will do something else. Is this possible?
+9
A:
You 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.
Prestaul
2008-10-05 00:41:52
That was it, thanks... I was trying document.getElementById('myDiv').onMouseDown = alert('test');You have an idea why that doesn't work?
mrjrdnthms
2008-10-05 00:46:04
Event handler properties in javascript must be assigned functions that act as handlers. When you assign event handler attributes inline in your html you do not use a function definition... Just one of those things!
Prestaul
2008-10-05 00:49:45