Search code examples
google-chromegoogle-chrome-devtoolsaccessibility

How to focus a specific element in Chrome?


I'm doing accessibility work and testing tab focus.

Is there an easy way skip focus to a specific element on chrome or any other browser? For example, there is a button in the middle of the page. How can I quickly focus that element without tabbing through everything before it first?


Solution

  • Just use the focus method in JavaScript:

    document.getElementById('myButton').focus();
    

    The focus method exists on all elements that are naturally focusable (like buttons, links, form elements), or that have been made focusable with the tabindex attribute.

    For input elements, you can also use the autofocus attribute to tell the browser to focus it as soon as the page is loaded.