Sina Fathieh
Sina Fathieh

Reputation: 1725

How do I check which elements have click binded to them with jquery?

I want to chck which elements in my page are clickable, meaning that they either have href attribute (which is easy to check), or they have click event binded to them via JS . is there any way to do so ?

Upvotes: 2

Views: 135

Answers (2)

MJJames
MJJames

Reputation: 745

using the solution provided for this question: How to debug JavaScript/jQuery event bindings with Firebug (or similar tool)

i think this may be what you need:

<html>
<head>
   <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script
</head>
<body>
    <a href="test">test link</a>
    <span>test span</span>
<script type="text/javascript">
$.fn.listHandlers = function(events, outputFunction) {
    return this.each(function(i){
        var elem = this,
            dEvents = $(this).data('events');
        if (!dEvents) {return;}
        $.each(dEvents, function(name, handler){
            if((new RegExp('^(' + (events === '*' ? '.+' : events.replace(',','|').replace(/^on/i,'')) + ')$' ,'i')).test(name)) {
               $.each(handler, function(i,handler){
                   outputFunction(elem, '\n' + i + ': [' + name + '] : ' + handler );
               });
           }
        });
    });
};


$("span").click(function(){ alert("test");});

var clickableObjects = [];
$(document).ready(function(){
    $('*').listHandlers('click',function(element,data){
        clickableObjects.push(element);
    }).filter("a").each(function(index, value){
        clickableObjects.push(value);
    });

});


</script>
</body>
</html>

Upvotes: 2

ghoppe
ghoppe

Reputation: 21784

Why not assign a .clickable class to elements with an href attribute and ensure you also assign a .clickable class to items when you .bind() them?

Upvotes: 0

Related Questions