I have a form with multiple submit buttons. Without using JavaScript, how can I specify which button is submitted when the user presses the Enter key?
-
possible duplicate of [How is the default submit button on an HTML form determined?](http://stackoverflow.com/questions/925334/how-is-the-default-submit-button-on-an-html-form-determined) – archil Dec 17 '11 at 07:51
2 Answers
In short, you need JavaScript. Each browser has its own default behavior when you press Enter when focused inside of a form element, and no two of these are exactly alike. The inclusion of multiple 'submit' buttons throws another layer of complexity into the equation that further cements my assertion that you need to write JavaScript to handle this behavior reliably. It wouldn't be any more complex than:
document.getElementById('focusedFormElement').onkeypress = function(e)
{
if (e.keyCode == 13)
{
document.getElementById('targetSubmitButton').click();
}
}
Just something to consider, if you haven't already.

- 5,137
- 1
- 18
- 20
I think it's better to avoid having your application work with the Enter key while you have multiple submit buttons. You may get problems when your users are working with it. However, I don't think you can do it without JavaScript (or jQuery). I suggest you think about what you can do to decrease users mistakes.
Good luck

- 19,179
- 10
- 84
- 156

- 12,864
- 27
- 75
- 127
-
It's fairly conventional to have a default button... One thing, though, could be that you do not have to worry about it too much because only Desktop users are "affected", smartphone users rarely have a Return key right at hand. – Alexis Wilke Aug 13 '17 at 02:51