Skip to content

getFocusableElements

Mike Byrne edited this page Mar 14, 2023 · 1 revision

description

Introduced 3.1.0

Returns a list of focusable elements within a target, optionally also including the target (if it itself is also focusable)

requires

  • isVisible (handled automatically)

parameters

  • el - required - DOM node to test
  • includeTarget - optional - boolean - defaults to false

returns

  • an array of focusable elements from within the target

example usage:

Imagine this example HTML:

...
<div id="modal">
  <button id="modalClose">Close Modal</button>
  <h1>Hello World</h1>
  <p>Lorem ipsum <a href="/">link</a> dolor sit amet...</p>
  ...
</div>
...
let modal = document.querySelector('#modal');
let focusable = getFocusableElements(modal);
// [button#modalClose, a]