Let's say you create a Wizard in an HTML form. One button goes back, and one goes forward. Since the back button appears first in the markup when you press Enter it will use that button to submit the form.
Example:
<form>
<!-- put your cursor in this field and press Enter -->
<input type="text" name="field1" />
<!-- This is the button that will submit -->
<input type="submit" name="prev" value="Previous Page" />
<!-- But this is the button that I WANT to submit -->
<input type="submit" name="next" value="Next Page" />
</form>
What I would like to do, is get to decide which button is used to submit the form when a user presses Enter. That way, when you press Enter the Wizard will move to the next page, not the previous. Do you have to use tabindex
to do this?
I came across this question when trying to find an answer to basically the same thing, only with asp.net controls, when I figured out that the asp button has a property called
UseSubmitBehavior
that allows you to set which one does the submitting.Just in case someone is looking for the asp.net button way to do it.
Using the example you gave:
If you click on "Previous Page" only the value of "prev" will be submitted. If you click on "Next Page" only the value of "next" will be submitted.
If however, you press enter somewhere on the form, neither "prev" nor "next" will be submitted.
So using pseudo code you could do the following:
I solved a very similar problem in this way:
if
javascript
is enabled (in most cases nowadays) then all the submit buttons are "degraded" to buttons at page load viajavascript
(jquery). Click events on the "degraded" button typed buttons are handled also viajavascript
.if
javascript
is not enabled then the form is served to the browser with multiple submit buttons. In this case hitting enter on atextfield
within the form will submit the form with the first button instead of the intended default, but at least the form is still usable: you can submit with both the prev and next buttons.working example:
Try this..!
You can use
Tabindex
to solve this issue, Also changing the order of button would be more efficient way to achieve this. Change the order of button and addfloat
values to assign them the desired position you want to show in yourHTML
view.