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 ?
Asked
Active
Viewed 130 times
2
-
It seems hard, and I doubt if it possible to accomplish what that you ask. – Fitzchak Yitzchaki Jan 25 '10 at 23:19
2 Answers
2
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>
0
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
- 21,452
- 3
- 30
- 21
-
I'm sure that the asker can accomplish what he want in a different way, but this is not what his asked. – Fitzchak Yitzchaki Jan 25 '10 at 23:15
-
So it's not useful to point out a simpler and faster approach to the problem in answering a question? – ghoppe Jan 26 '10 at 00:15
-
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 – Sina Fathieh Jan 26 '10 at 00:21