JSFiddle - React, Tailwind, and code Playground

by sitelint

HTML

<button type="button" id="buttonActionScroll">Activate me to focus on the button!</button>
<button type="button" id="buttonActionWithoutScroll">Activate me to focus on the button without scrolling!</button>

<div id="container" style="height: 1000px; width: 1000px;">
<button type="button" style="margin-top: 500px;" id="exampleActionButton">Action</button>
</div>

JavaScript

const exampleButton = document.getElementById('exampleActionButton');
const buttonActionScroll = document.getElementById('buttonActionScroll');
const buttonActionWithoutScroll = document.getElementById('buttonActionWithoutScroll');

function setFocus() {
  exampleButton.focus({
    preventScroll:false
  });
  
  console.log(document.activeElement.nodeName);
}
function setFocusWithoutScrolling() {
  exampleButton.focus({
    preventScroll:true
  });
  
  console.log(document.activeElement.nodeName);
}

buttonActionScroll.addEventListener('click', setFocus);
buttonActionWithoutScroll.addEventListener('click', setFocusWithoutScrolling);