4

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • http://stackoverflow.com/questions/1491718/jquery-more-than-one-handler-for-same-event this may help – Anoop Joshi P Jan 04 '16 at 09:19
  • *"I am trying to override the click event"* ... "*Is there any way to load event based on priority wise"* these statements are conflicting. It is not clear whether you want to override the previous handlers, or call the handlers in certain order based on priority. – T J Jan 05 '16 at 07:28
  • .. and I have no idea what this has to do with jquery-ui and javascript-objects.. – T J Jan 05 '16 at 07:29

4 Answers4

14

if I have understood correctly this works for you:

$('.xxx').off('click').on('click', function(e) {
    e.preventDefault();
}
Oscar LT
  • 787
  • 1
  • 4
  • 24
  • Thanks Oscar for your answer. Can you please tell me what above function will do? – Naresh Reddy Kallamadi Jan 04 '16 at 09:27
  • With "e.preventDefault()" the click becomes personalized, and now you can apply it you want, cancel the default use and you can write your function there. – Oscar LT Jan 04 '16 at 09:30
  • Thanks For all your answers, I am going to unbind or off the click functionality on selectors but those selectors are tabs using data toggles. so when we unbind or off click functionality, is the data toggle will work as expected? Means when I click on tab(using data toggle) it should work as expected but the click events should be unbind for that tag's selector. – Naresh Reddy Kallamadi Jan 04 '16 at 13:07
5

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!");
    });
Hardik
  • 228
  • 1
  • 6
  • Thanks For all your answers, I am going to unbind or off the click functionality on selectors but those selectors are tabs using data toggles. so when we unbind or off click functionality, is the data toggle will work as expected? Means when I click on tab(using data toggle) it should work as expected but the click events should be unbind for that tag's selector. – Naresh Reddy Kallamadi Jan 04 '16 at 12:42
1

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

$(SELECTOR).removeAttr('onclick');
tetta
  • 445
  • 4
  • 17
0

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.

Deepak Kothari
  • 1,601
  • 24
  • 31