javascript get focusable elements 1

javascript get focusable elements

/**
 * Gets keyboard-focusable elements within a specified element
 * @param {HTMLElement} [element=document] element
 * @returns {Array}
 */
function getKeyboardFocusableElements (element = document) {
  return [...element.querySelectorAll(
    'a, button, input, textarea, select, details,[tabindex]:not([tabindex="-1"])'
  )]
    .filter(el => !el.hasAttribute('disabled'))
}

Here is what the above code is Doing:
1. It’s getting all the focusable elements within the element passed in.
2. It’s filtering out any elements that have the disabled attribute.
3. It’s returning the array of elements.

Similar Posts