I have a form that has three submit buttons as follows:
<input type="submit" name="COMMAND" value="‹ Prev">
<input type="submit" name="COMMAND" value="Save">
<input type="reset" name="NOTHING" value="Reset">
<input type="submit" name="COMMAND" value="Next ›">
<input type="button" name="NOTHING" value="Skip ›" onclick="location = 'yada-yada.asp';">
The row of buttons is a mix of submit, reset and javascript buttons. The order of buttons is subject to change but in any case the save button remains between prev and next buttons. The problem here is that when a user hits "enter" to submit the form, the post variable "COMMAND" contains "Prev"; normal, as this is the first submit button on the form. I however want the "Next" button to be triggered when the user submits the form via enter button. Kind of like setting it as the default submit button, even though there are other buttons before it.
The first button is always the default; it can't be changed. Whilst you can try to fix it up with JavaScript, the form will behave unexpectedly in a browser without scripting, and there are some usability/accessibility corner cases to think about. For example, the code linked to by Zoran will accidentally submit the form on Enter press in a
<input type="button">
, which wouldn't normally happen, and won't catch IE's behaviour of submitting the form for Enter press on other non-field content in the form. So if you click on some text in a<p>
in the form with that script and press Enter, the wrong button will be submitted... especially dangerous if, as given in that example, the real default button is ‘Delete’!My advice would be to forget about using scripting hacks to reassign defaultness. Go with the flow of the browser and just put the default button first. If you can't hack the layout to give you the on-screen order you want, then you can do it by having a dummy invisible button first in the source, with the same name/value as the button you want to be default:
(note: positioning is used to push the button off-screen because
display: none
andvisibility: hidden
have browser-variable side-effects on whether the button is taken as default and whether it's submitted.)Set
type=submit
to the button you'd like to be default andtype=button
to other buttons. Now in the form below you can hit Enter in any input fields, and theRender
button will work (despite the fact it is the second button in the form).Example:
Tested in FF24 and Chrome 35.
If you're using jQuery, this solution from a comment made here is pretty slick:
Just add
class="default"
to the button you want to be the default. It puts a hidden copy of that button right at the beginning of the form.Quick'n'dirty you could create an hidden duplicate of the submit-button, which should be used, when pressing enter.
Example CSS
Example HTML
If someone now hits enter in your form, the (hidden) next-button will be used as submitter.
Tested on IE9, Firefox, Chrome and Opera
My suggestion is don't fight this behaviour. You can effectively alter the order using floats. For example:
with:
will effectively reverse the order and thus the "Next" button will be the value triggered by hitting enter.
This kind of technique will cover many circumstances without having to resort to more hacky Javascript methods.
Another solution, using jQuery:
This should work on the following forms, making "Update" the default action:
As well as:
This traps the Enter key only when an input field on the form has focus.