Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want HTML form submit to do nothing

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.

like image 500
Steven Biggums Avatar asked Aug 02 '10 04:08

Steven Biggums


People also ask

How do you make a form do nothing on submit?

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

How do you make HTML form not submit?

The simplest solution to prevent the form submission is to return false on submit event handler defined using the onsubmit property in the HTML <form> element.

Can a form have no action HTML?

A form without an action attribute is not a form, according to standards - and will actually cause a page reload in some browsers.. I've found that action="javascript:void(0);" works well.

How do I clear the form filled data after submitting the form in HTML?

Form reset() Method The reset() method resets the values of all elements in a form (same as clicking the Reset button). Tip: Use the submit() method to submit the form.


2 Answers

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> 
like image 76
Jeffrey Blake Avatar answered Oct 05 '22 23:10

Jeffrey Blake


<form id="my_form" onsubmit="return false;"> 

is enough ...

like image 31
simple Avatar answered Oct 05 '22 23:10

simple