79

I want an HTML form to do nothing after it has been submitted.

action=""

is no good because it causes the page to reload.

Basically, I want an Ajax function to be called whenever a button is pressed or someone hits Enter after typing the data. Yes, I could drop the form tag and add just call the function from the button's onclick event, but I also want the "hitting enter" functionality without getting all hackish.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steven Biggums
  • 793
  • 1
  • 5
  • 4
  • 3
    "i also want the "hitting enter" functionality without getting all hackish." i think that ship has sailed after you have decided to have a form with action="". why not just wire up handlers for onclick events, and keypress events? – RPM1984 Aug 02 '10 at 04:22

12 Answers12

109

By using return false; in the JavaScript code that you call from the submit button, you can stop the form from submitting.

Basically, you need the following HTML:

<form onsubmit="myFunction(); return false;">
    <input type="submit" value="Submit">
</form>

Then the supporting JavaScript code:

<script language="javascript"><!--
    function myFunction() {
        // Do stuff
    }
//--></script>

If you desire, you can also have certain conditions allow the script to submit the form:

<form onSubmit="return myFunction();">
    <input type="submit" value="Submit">
</form>

Paired with:

<script language="JavaScript"><!--
    function myFunction() {
        // Do stuff
        if (condition)
            return true;

        return false;
    }
//--></script>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeffrey Blake
  • 9,659
  • 6
  • 43
  • 65
20
<form id="my_form" onsubmit="return false;">

is enough ...

simple
  • 201
  • 2
  • 2
  • I don't think it'll work over a POST FIELD specially using JS to submit the form; can you give us a **jsfiddle demo** please? – PYK Apr 14 '20 at 17:04
  • This worked for removing the need for e.preventDefault(); – Zamicol Aug 23 '22 at 22:56
12

It also works:

<form id='my_form' action="javascript:myFunction(); return false;">
  • 4
    Although that doesn't do nothing, it the console it says `Uncaught SyntaxError: Illegal return statement`. – alejnavab Dec 30 '16 at 00:18
3
<form onsubmit="return false;">
Ali Jamal
  • 844
  • 11
  • 19
2

How about

<form id="my_form" onsubmit="the_ajax_call_function(); return false;">
 ......
</form>
ZX12R
  • 4,760
  • 8
  • 38
  • 53
2

You can use the following HTML

<form onSubmit="myFunction(); return false;">
  <input type="submit" value="Submit">
</form>
Rakhat
  • 4,783
  • 4
  • 40
  • 50
Keyur Shah
  • 1,335
  • 1
  • 11
  • 14
0

Use jQuery. Maybe put a div around your elements from the form. Then use preventDefault() and then make your Ajax calls.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt Phillips
  • 11,249
  • 10
  • 46
  • 71
0

Use:

<form action='javascript:functionWithAjax("search");'>
  <input class="keyword" id="keyword" name="keyword" placeholder="input your keywords" type="search">
  <i class="iconfont icon-icon" onclick="functionWithAjax("search");"></i>
</form>

<script type="text/javascript">
  function functionWithAjax(type) {
    // Ajax action

    return false;
  }
</script>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Junjie Li
  • 67
  • 1
  • 1
  • You should really explain the code a bit. It'll turn a good answer into a great one. – Neil Apr 09 '17 at 02:26
0

In cases where 'return false' doesn't do a thing,

try just passing the event to the form's onsubmit (not action!)

and simply add event.preventDefault() in that function.

Leave the 'action' from the html. Additional info: action="javascript:etc(event)" didn't work in my tests.

LuckyLuke Skywalker
  • 610
  • 2
  • 5
  • 17
-1

As part of the button's onclick event, return false, which will stop the form from submitting.

I.e.:

function doFormStuff() {
    // Ajax function body here
    return false;
}

And just call that function on submit button click. There are many different ways.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ross
  • 511
  • 3
  • 12
-1

You can use preventDefault to prevent the form's default submit behaviour from firing

form.addEventListener('submit', myHandler);


function myHandler(event) {
  event.preventDefault();

}
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Feb 03 '23 at 18:28
-2

Just replace 'action=""' withonsubmit='return false'. That's it.