Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two submit event handlers on a form. One must stop the other

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

like image 908
Silviu Postavaru Avatar asked Dec 22 '10 09:12

Silviu Postavaru


People also ask

How does the submit event work?

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.

Can I use the same event handler for multiple controls?

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.

What are the different types of event handlers?

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).

When does the submit event fire in a form?

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.


2 Answers

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 ;)

like image 87
Felix Kling Avatar answered Oct 09 '22 17:10

Felix Kling


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.

like image 45
Benubird Avatar answered Oct 09 '22 17:10

Benubird