I need to have a form submitted using the enter key, however, I have to use a BUTTON instead of SUBMIT as the type in order for the page to not refresh. How can I get my BUTTON to act as a SUBMIT and be executed whenever someone pushes their enter key?
<form>
<input type=text ...>
<input type=button ...>
</form>
A lot of the information I found about this mentions Netscape/IE/lots of outdated material.
This is my HTML output, I’m looking to hide the submit button and use ENTER:
Nah. Use a normal
submitbutton that refreshes the page. (And ideally, for accessibility, make it work!) Then add progressive enhancement to replace the submission action of the form with something smoother when JS is available. Usereturn false(orevent.preventDefault()in the DOM 2 Events model) to stop the form submitting in this case.Catching the
submitevent of a form is generally better than trying to pick upclickon buttons, because it will always fire when the form would normally be submitted, including on Enter keypresses.clickon the first submit button in a form will usually be fired on an Enter keypress, but there are cases (depending on number of controls in the form and what browser it is) where it doesn’t happen and so you can end up falling through to actually submitting the form.