views:

407

answers:

2

I'm currently working with a click event that fires an ajax call. I'd like to leverage a small plugin which, will handle some form data validation. The problem is that both the ajax call and the validation function of the plugin will be fired on the same click event.

In the end, I'd like the plugin to override the ajax function. I'm not really sure how to go about it. Any suggestions? I can't post the entire plugin, but here's a quick sample.

Ajax Call fired here:

$('p.thisClass').live('click',function() {
    //Ajax Fires here
});

Plugin snippet here:

config.button.live('click', function() {
    someFunction();
    // Stop Other click event from firing
});

There are a few ways to look at this, but in the end, I want the plugin to take precedence over the click that fires the ajax. Any thoughts on how to accomplish this??

Thanks

A: 

To cancel an event i jquery you just

return false;

You just have to check, which event handler to attach first.

A much better solution would however be to validate on change,not on submit. Doing so makes it much faster and less annoying for users to correct their bugs.

Thomas Ahle
Adding return false to the plugin will stop the plugin's click event. return false in the ajax function will stop that from firing. Neither accomplishes what I want. I want the plugin to fire and override any other click events on that element. These are two completely separate functions. This isn't a true form validation plugin and change validating on change isn't appropriate.
SDG
A: 

Do you have control of the binding function? If so, reference jQuery's unbind or die methods: Unbind Die

Using such, you can assign your original ajax click event to a variable and then unbind/die just that one:

Example: To unbind just one previously bound handler, pass the function in as the second argument:

     var foo = function () {
     // code to handle some kind of event
     };

     $("p").bind("click", foo); // ... now foo will be called when paragraphs
 are clicked ...

     $("p").unbind("click", foo); // ... foo will no longer be called.
neatlysliced