I would like to ask if an element will respond to a live event, without actually triggering that event.
HTML
<div id="foo">Click me!</div>
JS
$('#foo').live('mousedown', function() {
console.log('triggered mousedown event');
}
if ($('#foo').__willRespondToLiveEvent__('mousedown')) {
console.log('#foo is wired up properly');
}
This is a somewhat simple and contrived example, but I am looking for a replacement that actually works for __willRespondToLiveEvent__ psuedo code.
Is there anyway for jQuery to cough up this info without actually triggering the event?
If you want a utility to which you could pass a selector, you could do this:
Example: http://jsfiddle.net/NMBsG/3/
Or if you want to call it against a jQuery object, you could do this:
Example: http://jsfiddle.net/NMBsG/4/
Note that both of these only check against the selector, since that is how
.live()works. You could feasibly select the same element but using a different matching selector, and it would returnfalse. As such, the selector needs to be an exact match.EDIT:
Here’s a modified version that uses
.is()to test the jQuery object against the selectors that were given to.live()for the event type provided. As long as one element in the object matches a.live()handler for that event, it should returntrue.Example: http://jsfiddle.net/NMBsG/5/
EDIT: Fixed crash when there are no
.live()events assigned, thanks to @Gaby.