Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery trigger mousedown registered by addEventListener

I want to simulate fake mousedown event by jQuery trigger() method, and I register mousedown event by native javascript method - addEventListener(). And I found it can't be triggered...

elem.addEventListener('mousedown', function () {
    alert('addEventListener');
});
$(elem).on('mousedown', function () {
    alert('on');
});
$(elem).trigger('mousedown');

Sample in jsFiddle

I do some tests about it.

  1. Register mousedown event by jQuery on()
    • Result: works
  2. Register click event by addEventListener()
    • Result: works

Is anything wrong here?

Thanks.

P.S. The reason why uses addEventListener() is that I want to write a library without jQuery.

like image 371
Sam Avatar asked Jun 23 '14 02:06

Sam


People also ask

How do you trigger Mousedown?

The mousedown event occurs when the left mouse button is pressed down over the selected element. The mousedown() method triggers the mousedown event, or attaches a function to run when a mousedown event occurs. Tip: This method is often used together with the mouseup() method.

How do I use Mousedown event?

To cause a MouseDown event for a form section to occur, press the mouse button in a blank area of the form section. The following apply to MouseDown events: If a mouse button is pressed while the pointer is over a form or control, that object receives all mouse events up to and including the last MouseUp event.

What is the difference between Mousedown and click?

Note: This differs from the click event in that click is fired after a full click action occurs; that is, the mouse button is pressed and released while the pointer remains inside the same element. mousedown is fired the moment the button is initially pressed.

What is a Mousedown event?

MouseDown or MouseUp event procedures specify actions that occur when a mouse button is pressed or released. MouseDown and MouseUp events enable you to distinguish between the left, right, and middle mouse buttons.


2 Answers

Try using

EventTarget.dispatchEvent

MDN link

Your code will be something like

var elem = document.getElementById('square');
elem.addEventListener('mousedown', function () {
    alert('addEventListener');
});
$(elem).on('mousedown', function () {
    alert('on');
});

elem.dispatchEvent(new Event('mousedown'))

You might have to use fireEvent if you are considering IE.

object.fireEvent(bstrEventName, pvarEventObject, pfCancelled) 

MSDN link

It would be like

if (document.createEvent) {
    element.dispatchEvent(event);
} else {
    element.fireEvent("on" + event.eventType, event);
}
like image 146
Okky Avatar answered Oct 27 '22 22:10

Okky


Jquery Only triggers the events that created by itself

Even the order is also matters like : this Works

$(elem).on('mousedown', function () {
    alert('on');
});
$(elem).trigger('mousedown');

But Below code Will not Work

 $(elem).trigger('mousedown');
 $(elem).on('mousedown', function () {
    alert('on');
});

Take A look at This DEMO FIDDLE

like image 20
Maulik Anand Avatar answered Oct 27 '22 21:10

Maulik Anand