Skip to content

setFocusOnTarget

Mike Byrne edited this page Mar 14, 2023 · 3 revisions

description

Sets focus on a node

requires

  • nothing

parameters

  • node - required - DOM node to focus
  • exact - boolean - defaults to true - focus the exact mode, or focus the first focusable element within the target element

returns

  • nothing

example usage:

var button = document.querySelector('button');
setFocusOnTarget(button); // the button will be focused
var h1 = document.querySelector('h1');
setFocusOnTarget(h1); // the h1 will be focused
...
<div id="modal">
  <button id="modalClose">Close Modal</button>
  ...
</div>
...
var modal = document.querySelector('div#modal');
setFocusOnTarget(modal, false); // the button #modalClose will be focused
// if there is no focusable element inside the target, the target itself is focussed