Reputation: 64874
Is there a way in jQuery to detected if an element already has a bound event ?
In other words I want to bind an event only if the element hasn't bound it before.. otherwise I'm triggering it twice.
I'm using jQuery 1.2.7
Upvotes: 1
Views: 1928
Reputation: 1661
If anyone stumbles upon this post. This has been changed in the newer versions of jQuery.
The current code to access the list of events bound to an element is as follows...
Using an ID (2 ways):
$._data($('#IDValue')[0]).events
$._data($('#IDValue')[0], "events")
Using a class (2 ways):
$._data($('.classValue')[0], "events")
$._data($('.classValue')[0]).events
Upvotes: 1
Reputation: 14049
The answer given by Darin is the right one but in such cases, I prefer to add a class to the elements when the function is bound and check if the class is added when I am trying to bound to the elements
eg.
$('a').each(function(index, elem) {
if ($(elem).hasClass('added')) {
return;
}
else {
$(elem).click(function() {
alert("clicked");
})
.addClass("added");
}
})
Upvotes: 0
Reputation: 1039508
$('#someId').data('events')
should give you the events that are bound to this element.
Upvotes: 1