Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form with no action and where enter does not reload page

I am looking for the neatest way to create an HTML form which does not have a submit button. That itself is easy enough, but I also need to stop the form from reloading itself when submission-like things are done (for example, hitting Enter in a text field).

like image 791
Matt Roberts Avatar asked Nov 30 '09 07:11

Matt Roberts


People also ask

How do you make a form not reload the page?

Use jQuery's submit event to handle the form submit, add return false; at the end of the submit handle function to prevent the page to reload. return false ; });

What happens if form has no action?

If action is set to "" or if the action attribute is missing, the form submits to itself. That is, if your script is index.

Is form action required?

Yes, the form is required to have an action attribute in HTML4. If it's not set, the browser will likely use the same method as providing an empty string to it. You really should set action="" which is perfectly valid HTML4, follows standards, and achieves the same exact result.


2 Answers

You'll want to include action="javascript:void(0);" to your form to prevent page reloads and maintain HTML standard.

like image 71
Dutchie432 Avatar answered Sep 18 '22 14:09

Dutchie432


Add an onsubmit handler to the form (either via plain js or jquery $().submit(fn)), and return false unless your specific conditions are met.

Unless you don't want the form to submit, ever - in which case, why not just leave out the 'action' attribute on the form element?

like image 36
K Prime Avatar answered Sep 18 '22 14:09

K Prime