1

I need to check if some events are already bound on an element.

For example

$(".animate").click(function(){
    alert('Here comes action');
}); 

and

$(".someOtherSelector").click(function(){
    alert('some other action');
});

HTML

<a class="animate"></a>
<a class="someOtherSelector animate"></a>

In second function I need to check if there is an event already bound on this element. If so it should not execute alert('some other action');.

I'm using jQuery version 1.10.1.

insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
Leo Ray
  • 11
  • 1
  • 2
  • possible duplicate of [Is it possible to obtain a list of events bound to an element in jQuery?](http://stackoverflow.com/questions/1729732/is-it-possible-to-obtain-a-list-of-events-bound-to-an-element-in-jquery) – Denis Oct 04 '13 at 12:40
  • Actually i saw this question already. But in 1.10.1 $(".someOtherSelector").data('events') returns null. And if i using $(this) it returns null too – Leo Ray Oct 04 '13 at 12:44
  • See [this question](http://stackoverflow.com/questions/12214654/jquery-1-8-find-event-handlers) which explains how to do it on recent versions. – Denys Séguret Oct 04 '13 at 12:45

2 Answers2

4

As of jQuery 1.8, the event data is no longer available for data. Read this jQuery blog post. You should now use this:

jQuery._data(element, "events")

Code

$('.someOtherSelector').each(function(){
    console.log($._data(this, "events"));
});

Fiddle

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
2

you can get the events $(".someOtherSelector").data('events'), then check if the required event is present or not.

​var events = $._data( $(".someOtherSelector")[0], "events" );
if(events.indexOf("click") == -1) {
  $(".someOtherSelector").click(function(){
    alert('some other action');
  });
}
Ashis Kumar
  • 6,494
  • 2
  • 21
  • 36