user3273621
user3273621

Reputation: 85

Jquery:Unbind click event on the class

I am using jQuery and i want to unbind the click event of dom element wherever a particular class is added to that dom element.

This particular class is added and removed dynamically using add class and remove class functions. So if the class is added on dom element and id that dom element has a click event i want to unbind the click event.

Upvotes: 0

Views: 2331

Answers (3)

lebolo
lebolo

Reputation: 2160

I'm not sure what the context here is. How do you bind it in the first place and why do you want to unbind it?

An alternative way is to just ignore the click event if the element has a certain class.

$(selector).on("click", function(event) {
    // Returning false will automatically call event.stopPropagation() and event.preventDefault()
    if ($(this).hasClass("some-class")) return;

    // Other code
});

Upvotes: 0

Elias Zamaria
Elias Zamaria

Reputation: 101083

Why are you trying to bind and unbind the event? It may be simpler to do something like this:

$(document).on("click", "#element:not(.someClass)", function () {
    // this function will only run if the clicked element doesn't have the class someClass
});

Upvotes: 2

Amit Joki
Amit Joki

Reputation: 59252

How about this:

$('.someClass').unbind('click');

or

$('.someClass').off('click');

You have the answer in your title.

Upvotes: 2

Related Questions