Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 <form action="" onsubmit="myfunction()">.

Today I saw this, "<form action="" onsubmit="return false">". How does it work? I could not understand what is the meaning of onsubmit="return false".

PS: I found this when learning Ajax. It was a tutorial which explains how to submit data to a database without refreshing the page.

like image 466
Sasa1234 Avatar asked Jan 27 '16 12:01

Sasa1234


People also ask

What does Onsubmit mean in JavaScript?

The onsubmit event is an event that occurs when you try to submit a form. You can put your form validation against this event type.

What does return false do in JavaScript?

Web Developers use 'return false' in different ways. During form submission, if a particular entry is unfilled, return false is used to prevent the submission of the form.

What is Onsubmit return?

If you just call the function without the return in the onsubmit, then you're saying "execute this, but don't prevent the event if it return false." It's a way of saying execute this code when the form is submitted, but don't let it stop the event.

Why we use return false?

You use return false to prevent something from happening. So if you have a script running on submit then return false will prevent the submit from working. Show activity on this post. When a return statement is called in a function, the execution of this function is stopped.


3 Answers

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:

<script language="JavaScript">
myFunctionName() {
    if (document.myForm.myText.value == '')
        return false;
        // When it returns false - your form will not submit and will not redirect too
    else
        return true;
     // 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>

<form name="myForm" onSubmit="return myFunctionName()">
<input type="text" name="myText">
<input type="submit" value="Click Me">
</form>
like image 183
Amar Singh Avatar answered Oct 17 '22 18:10

Amar Singh


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

 <form name="myForm" onSubmit="myFunctionName(); return false">
    <input type="text" name="myText">
    <input type="button" value="Click Me" onclick="myFunctionName()">
 </from>
like image 28
Vikas Avatar answered Oct 17 '22 20:10

Vikas


According to the HTML standard, onsubmit is the name of an event handler. The event handler content attribute is treated as the FunctionBody of a JavaScript function which will be used to handle 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
    • If return value is false, then set event's canceled flag.

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

like image 8
AXO Avatar answered Oct 17 '22 20:10

AXO