JQuery: live events and querying if an element will respond to an event?

If you want a utility to which you could pass a selector, you could do this.

If you want a utility to which you could pass a selector, you could do this: Example: jsfiddle.net/NMBsG/3/ $('#foo'). Live('click', function() { alert('hi'); }); (function($) { $. HasLive = function(sel, type) { var data = $.

Data(document); if (data && data. Events && data. Eventstype) { var evts = data.

Eventstype; for (var I = 0, len = evts. Length; I HasLive('#foo', 'click')); Or if you want to call it against a jQuery object, you could do this: Example: http://jsfiddle.net/NMBsG/4/ $('#foo'). Live('click', function() { alert('hi'); }); (function($) { $.fn.

HasLive = function(type) { var data = $. Data(document); if (data && data. Events && data.

Eventstype) { var evts = data. Eventstype; for (var I = 0, len = evts. Length; I Selector) { return true; } } } return false; }; })(jQuery); alert($('#foo').

HasLive('click')); 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 return false.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 return true. Example: http://jsfiddle.net/NMBsG/5/ var test = $('#foo'). Live('click', function() { alert('hi'); }); (function($) { $.fn.

HasLive = function(type) { var data = $. Data(document); if (data && data. Events && data.

Eventstype) { var evts = data. Eventstype; for (var I = 0, len = evts. Length; I Selector) ) { return true; } } } return false; }; })(jQuery); alert($('#foo').

HasLive('click')); EDIT: Fixed crash when there are no .live() events assigned, thanks to @Gaby.

3 change the evts assignment to var evts = $. Data(document); and the if (evts) to if (evts && evts'events' && evts. Eventstype).

Otherwise it crashes if there is no live events assigned at all. – Gaby aka G. Petrioli Dec 23 '10 at 0:26 @Gaby: Very good point.

I hadn't considered that. I'll update. Thanks.

:o) – user113716 Dec 23 '10 at 0:27 Holy crap dude, not only does this work but it also gives a great level of insight into jQuery's innards. Thank you so much! – Squeegy Dec 23 '10 at 0:39 @Squeegy: You're welcome.

– user113716 Dec 23 '10 at 0:40.

Forget my answer.. missed the live part of the question .. check out patrick's answer var events = $('#foo'). Data('events'); if (events) { for (var event in events) console. Log('#foois wired up for the "'+event+'" event'); } this will print all the event that #foo is set to handle through jQUery.

For usage in an if var events = $('#foo'). Data('events'); if (events && events'click') { console. Log('#foo is wired up properly'); }.

Except that live() doesn't attach the event to the element. It is attached to the document, and uses the "#foo" selector to decide if it should fire. You would need to get the events for the document, then the type, then iterate over those.

I think the selector can be referenced by events. Click I .namespace. (Or perhaps 'selector' instead of 'namespace'.

Not sure which is better. ) – user113716 Dec 22 '10 at 23:59 @patrick a namespace can be added to any jQuery event, regardless of event type or element, so, without looking at the source code, I doubt namespace is the right property. Selector however, sounds right.

Come to think of it, your comment should be an answer. – Box9 Dec 23 '10 at 0:05 @box9: I just logged jQuery. Cache to the console, after assigning a .live() handler, and under the event, the #foo is assigned to both namespace and selector, but I think you're right that selector would make more sense.

I'll add an answer. Thanks. :o) – user113716 Dec 23 '10 at 0:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions