tags:

views:

43

answers:

2

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 ?

A: 

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?

ghoppe
I'm sure that the asker can accomplish what he want in a different way, but this is not what his asked.
Mendy
So it's not useful to point out a simpler and faster approach to the problem in answering a question?
ghoppe
Thank you for your reply, but I'm afraid Mendy is right, I want my script to be loaded externally from various pages, and once loaded, I want my script to detect which elements have "click" event binded to them
hakim-sina
+1  A: 

using the solution provided for this question: http://stackoverflow.com/questions/570960/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>
MJJames
thanks alot, $.fn.listHandlers function is the key :)
hakim-sina