Skip to content

Instantly share code, notes, and snippets.

@reoxb
Created April 10, 2023 15:48
Show Gist options
  • Save reoxb/6cf59bd1293f9f04aa669e4468de7eab to your computer and use it in GitHub Desktop.
Save reoxb/6cf59bd1293f9f04aa669e4468de7eab to your computer and use it in GitHub Desktop.
getKeyboardFocusableElements
/**
* 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
@reoxb
Copy link
Author

reoxb commented Apr 10, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment