Naresh Reddy Kallamadi
Naresh Reddy Kallamadi

Reputation: 123

How to override click event in jquery

I am working with JavaScript and JQuery, there is a reference for click event from jquery.js,

Here I am trying to override the click event, its not happened. Even though overridden still it is fairing old one not new one. Is there any way to load event based on priority wise?

Upvotes: 4

Views: 17574

Answers (4)

tetta
tetta

Reputation: 445

Maybe you need to "unbind" click event which defined as inline attribute. In this case you can use $.removeAttr() method.

$(SELECTOR).removeAttr('onclick');

Upvotes: 1

Hardik
Hardik

Reputation: 228

what you need to do is Call 'unbind' method first and then 'bind' method to write new click event of Jquery, and also make sure that all Jquery Plugins loaded properly, below is an example :

$("#button").unbind("click").bind("click", function(){
        alert("this is Overridden click event!");
    });

Upvotes: 5

Deepak Kothari
Deepak Kothari

Reputation: 1753

The .click() adds a new handler every time it's called, not overwrites an existing one.Execution will be in the order in which they were bind.

You can use $(id).unbind() to clear handlers on that element before adding the new one.

Upvotes: 0

Oscar LT
Oscar LT

Reputation: 797

if I have understood correctly this works for you:

$('.xxx').off('click').on('click', function(e) {
    e.preventDefault();
}

Upvotes: 15

Related Questions