Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery dispatchEvent wrapper method

Is there a wrapper method or some library for dispatchEvent in jquery? I've been looking for this on stackoverflow, but the closest method I've found is jquery's trigger(), which appears to only trigger jquery event listeners. Of course I could just use dispatchEvent myself, but I want to use jquery to make my code easier to read.

I'm writing a greasemonkey script, where I want to fire an event to some anonymous event listener. The page itself is not written in jquery.

Here's a jsfiddle link to explain what I'm trying to accomplish: https://jsfiddle.net/Zx3CA/

js:

function log(s){
    document.getElementById('log').innerHTML+=s+'<br/>';
}

$(function(){
    //code on the page, which shouldn't be changed
    //start
    document.getElementById('link').addEventListener('click',function(){
        log('click detected');
    },false);
    //end
    
    $('#triggerClickJQuery').on('click',function(){
        $('#link').trigger('click');
    });
    
    $('#triggerClickJS').on('click',function(){
        var event=document.createEvent('MouseEvents');
        event.initMouseEvent('click',true,true,null,-1,-1,-1,-1,0,true,false,false,true,0,null);
        $('#link').get(0).dispatchEvent(event);
    });
});

html:

<body>

<a id="link" href="javascript:void(0);">Link</a><br/>
<a id="triggerClickJQuery" href="javascript:void(0);">Trigger Click JQuery</a><br/>
<a id="triggerClickJS" href="javascript:void(0);">Trigger Click JavaScript</a><br/>

<div id="log"></div>

</body>

Thanks in advance.

like image 416
simonzack Avatar asked Sep 16 '13 08:09

simonzack


People also ask

What is use of dispatchEvent in Javascript?

Calling dispatchEvent() is the last step to firing an event. The event should have already been created and initialized using an Event() constructor. Note: When calling this method, the Event. target property is initialized to the current EventTarget .

How do you check if an event has been triggered in Javascript?

Click on the Event Listener Breakpoints, on the right side. Then perform the activity that will trigger the event, i.e. click if the event that used is click, double click if it is dblclick event.

What is trigger method in jQuery?

The trigger() method is a method in jQuery which is used to trigger a specified event handler on selected element. Syntax: $(selector).trigger(event, param1, param2) Note: Extra parameters can be passed in trigger() method. Example 1: This method triggered two methods to increase the value of method.

How do you trigger a function in Javascript?

The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.


1 Answers

The implementation of trigger in jQuery (upto 1.10.1) for custom events doesn't fire a native event and therefore you wont be able to listen for it via addEventListener or a different version of jQuery loaded on the same page.

This bug with all of its use-cases is well documented here: http://bugs.jquery.com/ticket/11047

As a solution you can use this plugin, which checks if the browser supports firing custom native events and uses it when available.

https://github.com/sandeep45/betterTrigger

like image 112
user566245 Avatar answered Sep 23 '22 01:09

user566245