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?
This is what i have tried out: 1. You need to make sure you give your buttons different names 2. Write an if statement that will do the required action if either button in clicked.
In PHP,
The first time I came up against this I came up with an onclick()/js hack when choices are not prev/next that I still like for its simplicity. It goes like this:
When either submit button is clicked it stores the desired operation in a hidden field (which is a string field included in the model the form is associated with) and submits the form to the Controller, which does all the deciding. In the Controller, you simply write:
You can also tighten this up slightly using numeric Operation codes to avoid the string parsing, but unless you play with Enums, the code is less readable, modifiable, and self-documenting and the parsing is trivial, anyway.
Would it be possible for you to change the previous button type into a button like this:
Now the Next button would be the default, plus you could also add the
default
attribute to it so that your browser will highlight it like so:Hope that helps.
Kevin,
This works without javascript or CSS in most browsers:
Firefox, Opera, Safari, Google Chrome all work.
As always, IE is the problem.
This version works when javascript is turned on:
So the flaw in this solution is:
Previous Page does not work if you use IE with Javascript off.
Mind you, the back button still works!
Sometimes the provided solution by @palotasb is not sufficient. There are use cases where for example a "Filter" submit button is placed above buttons like "Next and Previous". I found a workaround for this: copy the submit button which needs to act as the default submit button in a hidden div and place it inside the form above any other submit button. Technically it will be submitted by a different button when pressing Enter then when clicking on the visible Next button. But since the name and value is the same, there's no difference in the result.
It can work with CSS
Put them in the markup as the next button first, then the previous button next.
Then use CSS to position them to appear the way you want