1

I want to trigger an event using browser API (in javascript).

Just for test I'm using right now 2 ways and work correctly :

1)

var event = document.createEvent( "HTMLEvents" );
event.initEvent( "dblclick", true, false );
MyElement.dispatchEvent( event );

2)

var event = new Event( 'dblclick' );
event.initEvent("dblclick",true,false);
MyElement.dispatchEvent( event );

But initEvent and document.createEvent are deprecated methods (by Firefox).

There is any other way to trigger events (not to create new event and not using deprecated methods)?

Please no JQuery, pure javascript

If possible to illustrate with a simple example will be great.

Very simple example for demostration: http://jsfiddle.net/2w308jxt/1/

In this example an click initiate also a double click ;)

Thank You.

4
  • Did you see developer.mozilla.org/es/docs/DOM/elemento.addEventListener ? Commented Dec 17, 2014 at 18:45
  • Thanks but I don't want to register an event, I want to "trigger" an event already registered using Javascript: Example initiate an registered dblclick event (or maybe drop event ) from using just click. Commented Dec 17, 2014 at 18:49
  • 1
    MDN has also article about Custom Events ... Commented Dec 17, 2014 at 18:50
  • 1
    Thanks Teemu, I used Event() constructor to trigger events. My real application needs drop event but dataTransfer cannot be "emulated" (Security risks in the drag-and-drop model) so I decided to add a new attribute to event variable (created by Event()) an check (read) them by drop event function. Have a nice day. Commented Dec 18, 2014 at 0:14

1 Answer 1

2

I had the solution right before my eyes (Firefox only):

var event = new Event( 'dblclick', { 'view': window, 'bubbles': true, 'cancelable': true } );
MyElement.dispatchEvent(event);

Working example (Firefox only): http://jsfiddle.net/2w308jxt/2/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.