Skip to content Skip to sidebar Skip to footer

What Is The Meaning Of Onsubmit="return False"? (javascript, Jquery)

I know that the onsubmit event occurs when a form is submitted. Generally, we are calling a method on the onsubmit event, like
. Today

Solution 1:

This is basically done to handle the form submission via JavaScript.

For example - for validation purposes

See the below code and see how it can be beneficial:

<scriptlanguage="JavaScript">myFunctionName() {
    if (document.myForm.myText.value == '')
        returnfalse;
        // When it returns false - your form will not submit and will not redirect tooelsereturntrue;
     // When it returns true - your form will submit and will redirect// (actually it's a part of submit) id you have mentioned in action
}
</script><formname="myForm"onSubmit="return myFunctionName()"><inputtype="text"name="myText"><inputtype="submit"value="Click Me"></form>

Solution 2:

If you are using a button instead of submit as in my case below:

 <form name="myForm" onSubmit="myFunctionName(); return false">
    <inputtype="text" name="myText">
    <inputtype="button" value="Click Me" onclick="myFunctionName()">
 </from>

Solution 3:

This effectively prevents the form from being submitted in any circumstances except under script control (via form.submit(), which doesn’t trigger a submit event)

Reference : Query in Action THIRD EDITION (AFFECTING EVENT PROPAGATION AND SEMANTIC ACTIONS, Page no. 143)

Solution 4:

According to the HTML standard, onsubmit is the name of an event handler. The event handler content attribute is treated as a script. This script will be used to process the associated events. In step 5 of "The event handler processing algorithm", it says "Process return value as follows":

  • If event is a BeforeUnloadEvent object and event's type is beforeunload
    • In this case, the event handler IDL attribute's type will be OnBeforeUnloadEventHandler, so return value will have been coerced into either null or a DOMString.
    • If return value is not null, then:
      • Set event's canceled flag.
      • If event's returnValue attribute's value is the empty string, then set event's returnValue attribute's value to return value.
  • If special error event handling is true
    • If return value is true, then set event's canceled flag.
  • Otherwise

So, in short, return false will cancel the event (or in this case, cancels the form submission).

Post a Comment for "What Is The Meaning Of Onsubmit="return False"? (javascript, Jquery)"