I am improving accessibility in a web-app.
I would like to cycle through all potentially tabbable / focusable elements
I note that jQuery has its own pseudo-selector, :tabbable
but this isn't native.
I've never used jQuery much and I'm in no hurry to start.
I also note that in these two blog posts:
- 2017-07-17: How to get the first and last focusable elements in the DOM
- 2020-01-29: Getting keyboard-focusable elements
the (similar) solutions look like:
const keyboardfocusableElements = document.querySelectorAll(
'a[href], button, input, textarea, select, details, [tabindex]'
);
I'm guessing we might be able to add a few more items:
audio
button
canvas
details
iframe
input
select
summary
textarea
video
[accesskey]
[contenteditable]
[href]
[tabindex]
though perhaps some of these elements only become tabbable / focusable when they include the tabindex
attribute...?
Sticking with the list immediately above (for now), leaves us with the following:
const tabbableElements = document.querySelectorAll(
'audio, button, canvas, details, iframe, input, select, summary, textarea, video, [accesskey], [contenteditable], [href], [tabindex]'
);
which isn't terrible, but you'd be forgiven for thinking there might be something more elegant.
Is there a conciser approach to grabbing all potentially tabbable / focusable elements in a document?
Something like a CSS Level 4 pseudo-selector ? (I've not found anything along those lines...)