We can get all focusable elements using
querySelectorAll() and a comma-separated list of elements to target. We want to look for buttons, links, form eleements, and anything with a
tabindex that’s not
var focusable = document.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"]');
This returns a node list from which we can grab the first or last element as needed.
var focusable = document.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not(tabindex="-1"]'); var firstFocusable = focusable; var lastFocusable = focusable[focusable.length - 1];