views:

608

answers:

2

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).

+2  A: 

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?

K Prime
+1  A: 

Simply add this event to your text field. It will prevent a submission on pressing Enter, and you're free to add a submit button or call form.submit() as required:

onKeyPress="if (event.which == 13) return false;"

For example:

<input id="txt" type="text" onKeyPress="if (event.which == 13) return false;"></input>
GenericMeatUnit
Thankyou for that. I am afraid the clarity of your answer made clear to me the correctness of an earlier answer so you don't get the accepted answer but you do get my gratitude and an upvote!
Matt Roberts
Thank you. Glad to help. I was never one for the limelight anyway. ;)
GenericMeatUnit