HTML - Accesskey Attribute (keyboard shortcut)

HTML - Accesskey Attribute (keyboard shortcut)

About

All HTML elements may have the accesskey content attribute set.

The accesskey attribute's value 1) is used by the user agent as a guide for creating a keyboard shortcut that focuses (ie activates) the element.

Syntax

The value must be:

  • an ordered set of unique space-separated tokens
  • that are case-sensitive,
  • each of which must be exactly one Unicode code point in length.

Example

  • Two access key defined:
accesskey="s 0"
  • One
accesskey="A"

Assigned access key by Browsers

  • Chrome / Firefox: Alt + Shift + Key
  • Others: Alt + Key

Advanced

If you want to create advanced keyboard shortcut, you can also use the keydown event (or related wrapping library)

Example:

  • A simple search box form where we want to focus on
<form id="search-form">
     <span id="search-input-container">
          <input type="search" id="search-input" placeholder="Search ..." aria-label="Search for...">
     </span>
</form>
  • The javascript listen to keydown and if the control key and the key / are the one, we focus on the input
var searchInput = document.getElementById('search-input');
// window.parent because this code run in a iframe, otherwise just document
window.parent.addEventListener('keydown', function(event) {
        event.ctrlKey && event.key === '\\' && (event.preventDefault(),searchInput.focus())
})
  • Click Ctrl+\ to focus on the input





Discover More
Devtool Chrome Event Listener
DOM Event - Keydown

A Keydown is an input device event that tracks when a key goes down on the keyboard. keyup You can attach it to all element. This is also possible to create advanced accesskey (ie...
HTML - Document

An HTML document is a well-formed HTML string (ie that contains the html root element). web page The HTML textual representation can be stored: in a string in a file or in the body of an HTTP...
Devtool Track Active Element
HTML - Focus (Active element) (and Blur)

The focus is the interactive element actually active on a HTML dom document through: a mouse click (if you mouse click on a text input field, ready to begin typing, this element will come into focus)...
HTML - Global (Element) Attribute

The global-attributesglobal attributes are common attribute to all elements in the HTML language.
HTML - Keyboard Navigation (key event)

Navigation / Manipulation of an HTML document with the keys of a keyboard. The tab key is the default key that permits to move from a interactive element to another and therefore change the focus...



Share this page:
Follow us:
Task Runner