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.
You should not be using buttons of the same name. It's bad semantics. Instead, you should modify your backend to look for different name values being set:
Since I don't know what language you are using on the backend, I'll give you some pseudocode:
bobince's solution has the downside of creating a button which can be Tab-d over, but otherwise unusable. This can create confusion for keyboard users.
A different solution is to use the little-known
form
attribute:This is standard HTML, however unfortunately not supported in Internet Explorer.
I'm resurrecting this because I was researching a non-JavaScript way to do this. I wasn't into the key handlers, and the CSS positioning stuff was causing tab ordering to break since CSS repositioning doesn't change tab order.
My solution is based on the response at https://stackoverflow.com/a/9491141.
The solution source is below. tabindex is used to correct tab behaviour of the hidden button, as well as aria-hidden to avoid having the button read out by screen readers / identified by assistive devices.
1st button in DOM 2nd button in DOM 3rd button in DOM
Essential CSS for this solution: