Home > Enterprise >  Accesskey for "enter" keystroke in HTML
Accesskey for "enter" keystroke in HTML

Time:01-19

What is the accesskey for enter in html?

For example in a button to trigger it using the keystroke "h" it would be accesskey = "h" however accesskey = "enter" nor accesskey = "Enter" work

CodePudding user response:

You can not use accesskey to trigger a button with Enter key

From Mozilla Docs:

The attribute value must consist of a single printable character (which includes accented and other characters that can be generated by the keyboard).

You could use js to listen for an enter keypress pretty easily as follows:

const enterBtn = document.getElementById("enter-btn");

enterBtn.addEventListener("click", () => console.log("clicky!"));

window.addEventListener("keypress", ({key}) => {
  if (key == "Enter")
    enterBtn.click();
});
<p>If you need to relax, press the
<strong><u>S</u></strong>tress reliever!</p>

<button id="enter-btn">Stress reliever</button>

However what you're probably trying to recreate is the form action of submitting on enter, you can read more on the Mozilla Docs

CodePudding user response:

There is none. Per the MDN docs, "The attribute value must consist of a single printable character". The enter key is not a printable character.

It is also already in use as a keyboard shortcut for several significant UX interactions by default; assigning it to other uses would conflict with those expected defaults.

(Note also that this attribute "is seldom used because of its multiple conflicts with already present key bindings in browsers.")

  •  Tags:  
  • html
  • Related