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
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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.