Check if event exists on element

JavascriptJqueryJquery EventsEventtrigger

Javascript Problem Overview


Is there a way to check if an event exists in jQuery? I’m working on a plugin that uses custom namespaced events, and would like to be able to check if the event is bound to an element or not.

Javascript Solutions


Solution 1 - Javascript

$('body').click(function(){ alert('test' )})

var foo = $.data( $('body').get(0), 'events' ).click
// you can query $.data( object, 'events' ) and get an object back, then see what events are attached to it.

$.each( foo, function(i,o) {
    alert(i) // guid of the event
    alert(o) // the function definition of the event handler
});

You can inspect by feeding the object reference ( not the jQuery object though ) to $.data, and for the second argument feed 'events' and that will return an object populated with all the events such as 'click'. You can loop through that object and see what the event handler does.

Solution 2 - Javascript

You may use:

$("#foo").unbind('click');

to make sure all click events are unbinded, then attach your event

Solution 3 - Javascript

To check for events on an element:

var events = $._data(element, "events")

> Note that this will only work with direct event handlers, if you are using $(document).on("event-name", "jq-selector", function() { //logic }), you will want to see the getEvents function at the bottom of this answer

For example:

 var events = $._data(document.getElementById("myElemId"), "events")

or

 var events = $._data($("#myElemId")[0], "events")

Full Example:

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
	    <script>
	    	$(function() {
	    		$("#textDiv").click(function() {
	    			//Event Handling
	    		});
	    		var events = $._data(document.getElementById('textDiv'), "events");
	    		var hasEvents = (events != null);
	    	});
	    </script>
    </head>
    <body>
        <div id="textDiv">Text</div>
    </body>
</html>

A more complete way to check, that includes dynamic listeners, installed with $(document).on

function getEvents(element) {
	var elemEvents = $._data(element, "events");
	var allDocEvnts = $._data(document, "events");
	for(var evntType in allDocEvnts) {
		if(allDocEvnts.hasOwnProperty(evntType)) {
			var evts = allDocEvnts[evntType];
			for(var i = 0; i < evts.length; i++) {
				if($(element).is(evts[i].selector)) {
					if(elemEvents == null) {
						elemEvents = {};
					}
					if(!elemEvents.hasOwnProperty(evntType)) {
						elemEvents[evntType] = [];
					}
					elemEvents[evntType].push(evts[i]);
				}
			}
		}
	}
	return elemEvents;
}

Example usage:

getEvents($('#myElemId')[0])

Solution 4 - Javascript

use jquery event filter

you can use it like this

$("a:Event(click)")

Solution 5 - Javascript

I wrote a plugin called hasEventListener which exactly does that.

Hope this helps.

Solution 6 - Javascript

Below code will provide you with all the click events on given selector:

jQuery(selector).data('events').click

You can iterate over it using each or for ex. check the length for validation like:

jQuery(selector).data('events').click.length

Thought it would help someone. :)

Solution 7 - Javascript

This work for me it is showing the objects and type of event which has occurred.

    var foo = $._data( $('body').get(0), 'events' );
    $.each( foo, function(i,o) {
    console.log(i); // guide of the event
    console.log(o); // the function definition of the event handler
    });

Solution 8 - Javascript

I ended up doing this

typeof ($('#mySelector').data('events').click) == "object"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCorey HartView Question on Stackoverflow
Solution 1 - Javascriptmeder omuralievView Answer on Stackoverflow
Solution 2 - JavascriptToniView Answer on Stackoverflow
Solution 3 - JavascriptTom GView Answer on Stackoverflow
Solution 4 - JavascriptFareed AlnamroutiView Answer on Stackoverflow
Solution 5 - JavascriptSebastien P.View Answer on Stackoverflow
Solution 6 - JavascriptrohtakdevView Answer on Stackoverflow
Solution 7 - JavascriptJagtar SinghView Answer on Stackoverflow
Solution 8 - JavascriptGargamelView Answer on Stackoverflow