Using enterkeyhint Attribute to Build Better Mobile Forms

[object Object]
Sara MitevskaSeptember 16th, 2024
September 16th, 2024 6 minutes read
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5nWDoQIAHd8hxkf4JM8ELmDMEvmdGT7Lx1lhBQcJNXyBjbHm-ft9Gifg9pz1gLk2EN6qr908QaX9Z3iKVx4y2N4F6uEnPyPjmEYUuRaGXp15Tsb0HO1OIpfAI-5c3KVVtSoT8kaBX9afwNxZA6F5Ro_Tuh7zv4cwF4iBdsTSqMwlMIxurLpOYcr02hFOb/w400-h400/gst_bg_070_04.webp
enterkeyhint html attribute image

If you're looking to improve how you build your mobile forms, you've come to the right place! In our previous post, "Inputmode Attribute Explained: Key to Better Mobile Forms", we explored how the inputmode attribute can improve input fields for mobile devices. Today, we're continuing that story with another valuable attribute: the enterkeyhint. This small yet powerful attribute can greatly enhance the user experience and accessibility of your forms on mobile devices. Let’s explore how to use this attribute and learn how to create more intuitive and user-friendly forms.

What is enterkeyhint?

The enterkeyhint is a global HTML attribute that allows developers to tell the users what action will occur when they press the enter key on a virtual keyboard, such as submitting a form, moving to the next input field or inserting a new line. While inputmode controls what kind of virtual keyboard will be shown, enterkeyhint indicates how the enter key will be labeled (or which icon will be shown). By providing a clear and relevant label or icon, enterkeyhint helps guide users more effectively, making form interactions smoother and more intuitive, especially on mobile devices where keyboard space is limited.

How Does enterkeyhint Work?

The enterkeyhint works on any form fields such as input, textarea or any other element that is editable (has contenteditable=true). It is an enumerated attribute and only accepts the following values:

enterkeyhint="enter"

Indicates that pressing the enter key on a virtual keyboard will insert a new line. The label shown depends on user agent and user language but it is typically something like: ↵ or 'Return'. For example, if you have a multi-line text input (like a textarea) and you set enterkeyhint="enter", the virtual keyboard will display the enter key with a label indicating that it performs a line break or simply inserts a new line, rather than performing a specific action such as form submission or search.

enterkeyhint="done"

Indicates that pressing the enter key on a virtual keyboard will complete the current action, usually submitting a form. This hint is typically used in forms where the user is expected to finish entering data and submit it. For example, if you have a text input field in a form, setting enterkeyhint="done" will display a "Done" label on the enter key of the virtual keyboard, signaling that pressing it will submit the form or complete the current input.

enterkeyhint="go"

Indicates to the user that pressing the enter key on a virtual keyboard will trigger a "go" action. This is typically used in contexts where the user is expected to submit a search query or initiate a navigation action. For example, if you have a search input field, setting enterkeyhint="go" would display a "Go" label on the enter key of the virtual keyboard, signaling to the user that pressing it will start the search process.

enterkeyhint="next"

Indicates that pressing the enter key on a virtual keyboard will move the focus to the next input field or element in the form rather than submitting the form or performing another action. It would display a "Next" label on the enter key. This makes the navigation between fields more intuitive for users.

enterkeyhint="previous"

Indicates that pressing the enter key on a virtual keyboard will move the focus to the previous input field or element in the form. It would display a "Previous" label on the enter key. This is probably not that commonly used like the "next" hint, however is can be useful in multi-field forms where users may need to navigate backward through fields they have already filled out.

enterkeyhint="search"

Indicates that pressing the enter key on a virtual keyboard will trigger a search action. This is useful for input fields where users are expected to enter search queries. By setting enterkeyhint="search", the virtual keyboard will display a label such as "Search" on the enter key.

enterkeyhint="send"

Indicates that pressing the enter key on a virtual keyboard will trigger a send action. It would display a "Send" label on the enter key. This is useful in contexts where the user is expected to send a message or submit a communication.

If no enterkeyhint attribute is provided, the label on the enter key on the virtual keyboard defaults to the browser's or device's standard settings which may be generic, such as "Enter" or "Return" or if inputmode, type, or pattern attributes are used, the user agent might use contextual information from these attributes and display a suitable label or icon.

Note: Applying the enterkeyhint attribute only informs the user what will happen on press on the Enter key. To fully improve the user experience and the accessibility of your forms you must also implement the sutable functionality such as automatically navigating though the form fields on keypress or submitting the form on press on Enter.

Examples

Let's see it in action. Bellow is a multi-step form with three simple fields and a button that is responsible for submitting the form.

  <form>
    <input type="text" enterkeyhint="next" placeholder="First name" />
    <input type="text" enterkeyhint="next" placeholder="Last name" />
    <input type="text" enterkeyhint="done" placeholder="Address" />
    <button type="submit">Save</button>
  </form>

As users fill out each field and press enter, they are guided through the form fields in order due to the enterkeyhint="next" attribute. When they reach the last field, the enterkeyhint="done" attribute signals that pressing on Enter key at this point will submit the form.

Try out the example on your mobile device or using a virtual keyboard. Click on any field and notice the label in the right bottom corner.

It should look something like this. (This example is on iOS, the keyboard shown will vary depending on what type of device is used).

enterkeyhint keyboard example

You can also apply this attribute dynamically using the enterKeyHint property of on a HTMLElement


    const firstName = document.getElementById("firstName");
    const lastField = document.getElementById("lastName");

    firstName.enterKeyHint = "Next";
    lastField.enterKeyHint = "Done";

When to Use enterkeyhint

The enterkeyhint attribute is useful when you want to control the label or icon displayed on the "enter" key on virtual keyboards. You should use it in cases where the default behaviour doesn't fully match the user experience you're trying to provide. Here are situations where you can use its benefits:

  • Forms with Multiple Steps - As shown in the example above, when there are multiple fields or steps in a form and the functionality for field navigation is implemented, using enterkeyhint="next" or enterkeyhint="previous" can improve the user experience by informing them what will happen when they press Enter key.
  • Search Fields - Probably the most common scenario is to apply enterkeyhint="search" on a search input field that performs a search on press on Enter key.
  • Sending a Message- Similar to search fields, the attribute can be applied to an input or textarea fields that contain a message that will be sent on press on Enter key.
  • Custom Actions - Sometimes, even though the browser can infer an appropriate label, you might want to override it for a better user experience. For example, on a numeric input field, the default key might be "Next," but you want the key to indicate "Go" or "Done."

Browser support

enterkeyhint attribute is supported by most major browsers as shown on the table below.

title="https://caniuse.com/enterkey-hint"




Read next