Igor Golodnitsky
Igor Golodnitsky

Reputation: 4566

Jquery Selector that search for events

I need to select all elements, that has binded "click" event? Is there such selector exists?

Upvotes: 1

Views: 1381

Answers (3)

Marc Palau
Marc Palau

Reputation: 1

Search for the 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 use as has()

ex:

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

Upvotes: 0

ybo
ybo

Reputation: 17152

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

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

$("a:click").doStuff();

EDIT :

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

Upvotes: 6

Vasil
Vasil

Reputation: 38116

No. You can iterate over all elements and check if they have an event binding. But that wouldn't be very efficient unless you have a clue what kind of elements would have that event binding so you can narrow the search.

Upvotes: 0

Related Questions