Jump to content

set if statement for mousedown


UrbanDweller

Recommended Posts

Hey,

 

My question is, is it possible to create an if statement for div.onmousedown that will then excute document.onmousemove call function.

 

var div = document.getElementById('div');
document.onmousedown = eventHandler();
function eventHandler(){
     if(div.onmousedown){
    document.onmousemove = moveFunction();
    }
}

 

The reason I ask this is that i have a script that moves a div in a certain area and need the div to move even if the mouse is not inside it. This mouse down function is not the only one so i need something that can work with multiple mouse functions depending on wat one wat clicked etc

Link to comment
https://forums.phpfreaks.com/topic/259927-set-if-statement-for-mousedown/
Share on other sites

If I understand you right, you just need to assign the same function to multiple events. I'm not sure in that case why you would need to determine which event was fired though? If different events achieve the same outcome but do things slightly different, you should define an additional function(s) to handle chunks of the process that your event handlers can use, to avoid repetition.

 

The code you provided is almost right for assigning a function to an event, except there's a subtle problem.

 

document.onmousedown = eventHandler();

 

By including the parentheses () on the end, you're invoking the function immediately and assigning the result to the event. You should just pass the function name:

 

document.onmousedown = eventHandler;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.