I have to event handlers attached to a form. The first one that fires should stop the other event if a condition is not met.
The code below does not work, as both events will be triggered. Can you help me?
Thanks!
//fires first
$("#myform").submit(function(){
if (some validation) {
alert("You need to make the form valid");
return false;
}
});
//fires second
$("#myform").submit(function(){
//ajax stuff
return false;
});
p.s. I have to this as the ajax stuff is in a plugin that is not changeable. I cannot avoid two event handlers
The submit event fires when the user clicks a submit button ( <button> or <input type="submit">) or presses Enter while editing a field (e.g. <input type="text">) in a form. The event is not sent to the form when calling the form.submit () method directly.
You can also use the same event handler to handle the same event for different controls. For example, if you have a group of RadioButton controls on a form, you could create a single event handler for the Click event and have each control's Click event bound to the single event handler.
Event Handlers 1 Basic Events. When a user triggers an event, your browser usually does something in response. ... 2 Form Events. The following events usually occur when a user is interacting with a form. ... 3 Modern Events. In most modern browsers you can assign event handlers to any HTML element (in the beginning it was just links and forms).
The submit event fires when the user clicks a submit button (<button> or <input type="submit">) or presses Enter while editing a field (e.g. <input type="text">) in a form. The event is not sent to the form when calling the form.submit () method directly. Note: Trying to submit a form that does not pass validation triggers an invalid event.
Have a look at event.stopImmediatePropagation()
:
Keeps the rest of the handlers from being executed and prevents the event from bubbling up the DOM tree.
$("#myform").submit(function(event){
if (some validation) {
event.stopImmediatePropagation();
alert("You need to make the form valid");
return false;
}
});
You might also want to use event.preventDefault()
.
Update: Just to clarify: You can rely on the order the event handlers are called. From jQuery's bind
method:
When an event reaches an element, all handlers bound to that event type for the element are fired. If there are multiple handlers registered, they will always execute in the order in which they were bound. After all handlers have executed, the event continues along the normal event propagation path.
The order might not be defined in W3C's original definition but it works with jQuery. Otherwise, the above named function would be unnecessary anyway ;)
In short, no. If you have two event handlers, then you have two event handlers. That said however, there are ways around it.
First, remember that javascript execution is single-threaded. If you define a global variable, say var haveCalledSubmit=0;
, then you can use that to synch your handlers. For example, if you start each handler function with this:
if(haveCalledSubmit == 1)return haveCalledSubmit = 0;
else haveCalledSubmit = 1;
Then only one of the two handlers will be called. You can easily modify it to match some condition, or to deal with more than two functions.
An alternative is to look into event propagation models. This page will give you all the information you need, although Felix has already mentioned an ajax command that may do what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With