HTML DOM accessKey Property
The DOM accessKey property is used to set or return the accesskey attribute of an element.
Syntax :
- For Set the accessKey :
HTMLElementObject.accessKey = value
- For Return the accessKey :
HTMLElementObject.accessKey
Value:
- character: 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.
html
<!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 > < a id = "main" href = "https://www.w3wiki.net" > w3wiki </ a > < br > < p > This example show how to attach an accessKey to a selected element. </ p > < script > document.getElementById("main").accessKey = "g"; </ script > </ body > </ html > |
Output:
Example 2: This example shows how to check which accessKey is attached to a selected element.
HTML
<!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.w3wiki.net" > w3wiki </ a > < p > Click the button to get the accesskey of the link. </ p > < p id = "p1" >Answer : < span id = "d" > </ span > </ p > </ div > < br > < input type = "button" onclick = "myFunction()" value = "Click Me.!" /> < script > function myFunction() { let gfg = document.getElementById("main").accessKey; document.getElementById("d").innerHTML = gfg; } </ script > </ body > </ html > |
Output:
Note: The shortcut varies from browser to browser.
- Mozilla Firefiix: ALT + SHIFT + accesskey
- Google Chrome: ALT + accesskey
- Opera 15+: ALT + accesskey
- Safari: ALT + accesskey
- IE: ALT + accesskey
Supported Browser: The browser supported by DOM accessKey Property are listed below.
- Google Chrome 17 and above
- Edge 12 and above
- Internet Explorer 5.5 and above
- Firefox 5 and above
- Opera 12.1 and above
- Safari 6 and above