Just use tabindex
and autofocus
HTML Attributes
(Do not use Javascript for this solution!!)
Newer evergreen browsers and older browsers automatically submit the first form submit button they find in the web page when the ENTER/RETURN key is pressed by a user. NO JAVASCRIPT NEEDED! Browsers have worked this way for nearly 20 years but new web developers have not bothered to learn about it.
If you have multiple submit buttons in a web page and want to control which submit button is pressed, its easy to do without JavaScript. Just set the tabindex
to draw focus to the first form field set of controls that contains a submit button! Pressing the ENTER/RETURN keys will **trigger submission of form data when any form field control in the web page that is associated with a submit
type button
for that form
has focus by the user.
To help with this focus, use the autofocus
attribute set on any form field input
in the form
. The submit button for that form holding the input will then trigger when the ENTER/RETURN key is pressed. Otherwise, as mentioned above, pressing ENTER or RETURN on the keyboard automatically triggers the first available submit button for the page.
How to Combine tabindex
with autofocus
Instead of JavaScripting focus, an easier solution is to just add tabindex=0
on any of your form fields or submit buttons inside a form element first, then autofocus
on the first input control or submit button in your form and page. tabindex
controls the order of which form fields and buttons get focus first. It is built into all browsers, old and new.
Tabindex=0
assigns equal focus to every input and button in the page's list of indexed tab order page items. This makes sure a new tab order form field list is reordered and explicitly focused on first. autofocus
shifts focus to your chosen form field. Because the focus is now on this tabindex
grouping inside one of your forms
, pressing enter triggers the first submit button in that grouping to respond to the ENTER/RETURN key command.
The user can now press "ENTER" on their keyboard to submit the form at any point they like. This also has the advantage that the first form field is focused on and ready for data by the user.
Additional: You can enter tabindex=-1
on any buttons in the page to disable them from being in a tab index set or having tab focus. This also prevents them from being selected.
Lastly, never have orphaned input controls without a form
HTML wrapper, like you have shown. Its not just broken HTML, but semantically wrong. HTML was designed to hold multiple HtML form tags. Because you can have multiple forms in a web page, you use <form>
tags to control which form button goes with which form, or the "form" attribute set to the parent form holding it, as well as control user focus. Because you are using JavaScript to capture all form submissions, it does not matter what type
(attribute) of buttons you use, or how you handle the scripts. All that matters is which ones will have FOCUS FIRST in the list.
FYI: <button>
HTML elements always default to "submit"
types if you do not add a type. Submit type buttons submit to the server, but JavaScript can easily shut that process down and stop submission. So it does not matter, except for the fact only submission types reliably respond to the ENTER event. You can use type=button
for the type, which is just a non-submitting button. But I would stick with type=submit
or no type which is the same (default) to avoid having type=button
ones fail in some browsers.
An example below:
<form id="buttonform2" name="buttonform2" action="#" method="get" role="form">
<label for="username1">Username</label>
<input type="text" id="username1" name="username" value="" size="20" maxlength="20" title="Username" tabindex="0" autofocus="autofocus" />
<label for="password1">Password</label>
<input type="password" id="password1" name="password" size="20" maxlength="20" value="" title="Password" tabindex="0" role="textbox" aria-label="Password" />
<button type="submit" id="button2" name="button2" type="submit" value="submit" form="buttonform2" title="Submit" tabindex="0" role="button" aria-label="Submit">Submit</button>
</form>
Stop using JavaScript for simple solutions HTML can solve. HTML attributes used in web browsers have had this native ability for over 20 years. New web developers should know this.