Created
April 10, 2023 15:48
-
-
Save reoxb/6cf59bd1293f9f04aa669e4468de7eab to your computer and use it in GitHub Desktop.
getKeyboardFocusableElements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets keyboard-focusable elements within a specified element | |
* @param {HTMLElement} [element=document] element | |
* @returns {Array} | |
*/ | |
function getKeyboardFocusableElements(element = document) { | |
return [ | |
...element.querySelectorAll( | |
'a[href], button, input, textarea, select, details,[tabindex]:not([tabindex="-1"])' | |
), | |
].filter( | |
el => !el.hasAttribute('disabled') && !el.getAttribute('aria-hidden') | |
) | |
} | |
// display:none - embed, object, iframe add to filter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://zellwk.com/blog/keyboard-focusable-elements/