What is the meaning of onsubmit="return false"? (JavaScript, jQuery)

I know that the onsubmit event occurs when a form is submitted.

Generally what we do is, we are calling a method on onsubmit event like <form action="" onsubmit="myfunction()">

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

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

Answers


This basically done to handle the form submission via JavaScript.

For example - for validation purpose See below code and see how it can be beneficial:

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

If you are using 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()">
 </FORM>

Need Your Help

How come some site urls do not include a file extension?

url file-extension

I was browsing the internet and noticed, YouTube, for example, contains a URL like this to denote a video page: http://www.youtube.com/watch?v=gwS1tGLB0vc.

Where are Rails 3 custom JavaScript events defined?

ruby-on-rails-3 ujs

As I look through the Rails 3 jquery-ujs code, I notice that it binds to custom JavaScript events (submit.rails, click.rails, etc). Does anyone know where are these custom '.rails' events defined? ...