Leo Ray
Leo Ray

Reputation: 11

JQuery check events binding

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.

Upvotes: 1

Views: 9852

Answers (2)

Rohan Kumar
Rohan Kumar

Reputation: 40639

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

Upvotes: 4

Ashis Kumar
Ashis Kumar

Reputation: 6554

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');
  });
}

Upvotes: 2

Related Questions