jQuery plugin which checks if an Object or a DOM element actually has a particular/live/delegated event listener bound to it.
"hasEventListener" is a (about 2kB minified and 1kB gzipped) jQuery plugin which checks if an Object or a DOM element actually has a particular/live/delegated event listener bound to it. As a bonus, "hasEventListener" also exposes two new jQuery methods called "getEventsData" whose can be used as a 1.2.3+ compatible way to get events data internally stored on an element by jQuery (please, see related examples and API/documentation).
by vijayweb
HTML
<script src="https://github.com/sebastien-p/jquery.hasEventListener/raw/master/jquery.hasEventListener-2.0.3.min.js"></script>
<div id="test">Testing div</div>
<hr/>
<ul id="log"></ul>
CSS
div {
background: #eee;
border: 1px dashed #333;
margin: 15px;
padding: 15px
}
JavaScript
/*
Copyright (c) 2010 Sebastien P.
http://twitter.com/_sebastienp
http://github.com/sebastien-p/jquery.hasEventListener
http://jsfiddle.net/sebastienp/eHGqB/
MIT licensed.
---
Version 2.0.3 - Mar. 10, 2011.
"hasEventListener" is a (about 2kB minified and 1kB gzipped)
jQuery plugin which checks if an Object or a DOM element
actually has a particular event listener bound to it.
As a bonus, "hasEventListener" also exposes two new jQuery
methods called "getEventsData" whose can be used as a 1.2.3+
compatible way to get events data internally stored on an element
by jQuery (please, see related examples and API/documentation).
Examples/Usage :
- $.hasEventListener($("#tabs li")[0], "!delegate click.tab_widget");
--> returns true or false.
- $("#tabs li:hasEventListener(!delegate click.tab_widget)");
--> returns a jQuery object.
- $("#tabs li").hasEventListener("!delegate click.tab_widget");
--> returns a jQuery object.
- $.hasEventListener($({}).bind("custom_event", function () {})[0], "custom_event");
--> returns true.
- $({}).bind("custom", function () {}).hasEventListener("custom");
--> returns a jQuery object.
- $.getEventsData(window, "scroll");
--> returns an object or undefined.
- $("*").getEventsData();
--> returns an object or undefined.
*/
$(function () {
var $log = $("#log"),
$test = $("#test").bind("click.bind", handler);
function log(message, method) {
$log[method || "append"]("<li>" + message + "</li>");
}
function handler(e) {
log(+new Date() + " : " + e.type, "prepend");
}
$.fn.live && $test.live("mouseenter.live", handler);
$.fn.delegate && $("body").delegate("#test", "mouseleave.delegate", handler);
$.each([
"!live",
"!delegate",
".bind",
"!live .live",
"!delegate mouseleave",
"!delegate"
], function (index, value)...