JQuery Selector that searches for events

Do I need to select all elements attached to the click event? Is there such a selector?

+1
source share
3 answers

It is not natively supported by jQuery, but you can write your own custom selector using the hasEvent plugin :

jQuery.expr[":"].click = "jQuery(a).hasEvent('click');"; $("a:click").doStuff(); 

EDIT:

There is also an Event Bound Selector plugin that is more complete and works out of the box, but also more.

+6
source

Not. You can iterate over all elements and check for event binding. But this will not be very effective if you do not understand which elements will be bound to the event so that you can narrow your search.

0
source

Search Position:

  button: function(elem){ return "button" === elem.type || elem.nodeName.toLowerCase() === "button"; }, input: function(elem){ return /input|select|textarea|button/i.test(elem.nodeName); }, //init custom ev: function(elem,i,match){ var what = $(elem).hasEvent(match[3]); var type = (typeof what); return what !== null && type !== undefined; } //End custom }, setFilters: { first: function(elem, i){ return i === 0; }, last: function(elem, i, match, array){ return i === array.length - 1; }, even: function(elem, i){ return i % 2 === 0; }, 

.....

same usage as has()

Example:

 $('form:ev(submit)'); $('input:ev(click)'); $('a:ev(click)'); 
0
source

Source: https://habr.com/ru/post/1395877/


All Articles