UrbanDweller Posted March 29, 2012 Share Posted March 29, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259927-set-if-statement-for-mousedown/ Share on other sites More sharing options...
Adam Posted March 30, 2012 Share Posted March 30, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/259927-set-if-statement-for-mousedown/#findComment-1332747 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.