I am trying to force mouseup event after mousedown

elements[0].onmousedown = function(){
      console.log('screen clicked.');
      simulatemouseuphere;
};

      

All I have to do is add something to my function that triggers the mouse even if the user holds down the click button.

Elements [0] are my entire page area, so wherever you click it fires.

+3


source to share


4 answers


I'm sure you can just call the onmouseup event ...

elements[0].onmousedown = function(){
      console.log('screen clicked.');

      elements[0].onmouseup();
};

      



Be aware that when you physically stop holding the mouse, the mouseup event will fire again. (if you fail in onmouseup () event)

Tested quickly, it looks like it works: http://jsfiddle.net/cUCWn/40/

+1


source


Easy to guess, but instead of triggering the mouseup event, perhaps you want to call the function associated with the mouseup event?



var onMouseUp = function() { 
 /* something */  
};

var onMouseDown = function() { 
 /* something */ 
 onMouseUp.apply(this,arguments); 
};

elements[0].onmousedown = onMouseDown;
elements[0].onmouseup = onMouseUp;

      

+1


source


$("body").on("mousedown", function(e)
{
    e.type = "mouseup";
    $(this).trigger(e);
}).on("mouseup", function(e)
{
    console.info(e);
});

      

Or: fooobar.com/questions/200761 / ...

For pure JS: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events#Triggering_built-in_events

0


source


All the methods described here depend on how you set up the listener: the jQuery property, element.

If you want a reliable way that will work no matter how the event was set up use How can I trigger a click on a JavaScript event

elements[0].onmousedown = function(){
    fireEvent(elements[0], 'onmouseup');
};

      

Note that you are probably better off turning off the mouseup handler behavior in a function that you can call. Launch handlers are usually code smells because you don't always know what else you might be triggering when events are fired. For example:

function doSomethingOnMouseUp() {
    console.log('something on mouse up');
}

elements[0].onmousedown = function(){
    doSomething();
    doSomethingOnMouseUp();
};

elements[0].onmouseup = doSomethingOnMouseUp;

      

0


source







All Articles