views:

2110

answers:

1

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
That was it, thanks... I was trying document.getElementById('myDiv').onMouseDown = alert('test');You have an idea why that doesn't work?
mrjrdnthms
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