In this shot, we will learn how to activate or focus an HTML DOM
element without clicking the event or using a mouse. We will create a unique shortcut key to focus a DOM element; an accesskey
is a keyboard key assigned to a DOM element.
DOM elements can be any elements on an HTML page like paragraph, button, body, etc.
How to use the shortcut key in different browsers:
In Windows:
[ALT] + accesskey
[SHIFT] [ESC] + accesskey
[ALT] [SHIFT] + accesskey
In Mac:
[Control]
+[Option]
+ accesskey
In Linux:
[Alt]
+ accesskey
[ALT] [SHIFT] + accesskey
Before you press the accesskey
, click on the HTML page text to make the Educative window active and perform the accesskey
operation.
We will create a shortcut key to activate the DOM element in the above HTML and JavaScript code.
In line 5, in the <body>
tag of the HTML page, we defined an anchor tag which on-click will take you to www.abc.com, and we will do this with the assigned accesskey ‘w’
.
In line 10, at the end of the <body>
tag, we wrote JavaScript code to assign a unique accesskey ‘w’
. We used document.getElementById()
to get the DOM element and assigned the accesskey ‘w’
using .accessKey property
.
On pressing alt + w
, we see that the link www.abc.com has been activated without clicking the link.
If it does not work:
alt + w
key.alt
if you are on a different browser.RELATED TAGS
CONTRIBUTOR
View all Courses