The DOM accessKey property is used to set or return the accesskey attribute of an element.Â
Syntax
- To set the accessKey
HTMLElementObject.accessKey = value- To return the accessKey
HTMLElementObject.accessKeyValue: A character that is used to specify the shortcut key.
Return Value: This will return the selected element with the attached access key.Â
Example 1: This example shows how an accessKey is attached to a selected element.Â
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>
HTML DOM accessKey Property
</title>
<style>
body {
width: 90%;
color: green;
border: 2px solid green;
height: 40%;
font-weight: bold;
text-align: center;
padding: 30px;
font-size: 20px;
}
</style>
</head>
<body>
<!--Driver Code Ends-->
<a id="main" href="https://www.geeksforgeeks.org">
GeeksforGeeks
</a>
<br>
<p>
This example show how to attach
an accessKey to a selected element.
</p>
<script>
document.getElementById("main").accessKey = "g";
</script>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
Example 2: This example shows how to check which accessKey is attached to a selected element.Â
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>
HTML DOM accessKey Property
</title>
<style>
div {
width: 80%;
color: green;
border: 2px solid green;
height: 40%;
font-weight: bold;
text-align: center;
padding: 30px;
font-size: 20px;
}
#p1 {
width: 130px;
height: 20px;
display: block;
background-color: lightgrey;
float: right;
margin-top: -10px;
padding: 5px;
}
#d {
color: black;
}
</style>
</head>
<body>
<div>
<a id="main"
accesskey="g"
href="https://www.geeksforgeeks.org">
GeeksforGeeks
</a>
<p>
Click the button to get the
accesskey of the link.
</p>
<p id="p1">Answer : <span id="d">
</span>
</p>
</div>
<br>
<!--Driver Code Ends-->
<input type="button"
onclick="myFunction()"
value="Click Me.!" />
<script>
function myFunction() {
let gfg =
document.getElementById("main").accessKey;
document.getElementById("d").innerHTML = gfg;
}
</script>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
- element.accessKey lets you assign (or read) a single character that can be used as a keyboard shortcut to focus or activate the element. Browsers usually require a modifier key (e.g. Alt + key, or Alt + Shift + key) to trigger it, because the bare key would conflict with normal typing.
- Access keys frequently clash with browser, operating system, or assistive-technology shortcuts. Behavior and required modifier keys also vary significantly across browsers and platforms, making the feature unreliable. Because of these conflicts, modern accessibility guidelines generally advise against using accesskey / accessKey for general web content.