CKTIS / JavaScript
ARIA invalid and hidden states and focus management with tabindex
Report validation errors, hide decorative content from assistive technology, and move keyboard focus without adding an extra stop to the Tab sequence.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
HTML attributes: aria-invalid="true|false|grammar|spelling"; aria-hidden="true|false"; aria-errormessage="message-id"; hidden; tabindex="integer".
JavaScript DOM: control.setAttribute("aria-invalid", "true"); control.removeAttribute("aria-invalid"); message.hidden = false; message.tabIndex = -1; message.focus(); document.activeElement === message.ARIA invalid/hidden state and tabindex focus management separates three jobs: reporting errors, controlling accessibility exposure, and moving focus. aria-invalid reports a validation result to assistive technology; it does not validate a value or block submission. Set it to "true" when validation finds an error, but wait for a submission attempt before marking an empty required field. After correction, remove the attribute or set it to "false", the default. "grammar" and "spelling" identify grammar and spelling errors. With "true", you can use aria-errormessage to link to a visible correction message by its ID.
aria-hidden="true" excludes an element and its descendants from the accessibility tree without visually hiding them or disabling focus. Use it for decorative or redundant content, never on a focusable element or its ancestor. aria-hidden="false" cannot undo an ancestor’s hidden state. To hide an ordinary message from everyone, set message.hidden = true; set it to false to reveal the message.
The DOM property tabIndex corresponds to the HTML attribute tabindex. With the attribute omitted, native controls keep their default focus behavior. 0 puts an element in the normal sequence, usually in document order. -1 allows focus() but normally skips the element during Tab navigation. Avoid positive values: within the same focus-navigation scope, they put elements ahead of ordinary stops in ascending numeric order. Browser and platform settings can affect the sequence.
Reveal a target before calling focus(). If you are hiding an element that has focus, move focus somewhere useful first. In the example, noValidate bypasses automatic validation on submission, but validity.valueMissing still detects the empty required field. These are web-platform features, not ECMAScript features. document.activeElement checks DOM focus, not what a screen reader will announce.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Form validation and focus</title>
</head>
<body>
<script>
(() => {
const form = document.createElement('form');
form.noValidate = true;
const input = document.createElement('input');
input.required = true;
const label = document.createElement('label');
label.append('Name ', input);
const error = document.createElement('p');
error.id = 'ck-focus-name-error';
error.textContent = 'Enter your name.';
error.hidden = true;
error.tabIndex = -1;
const submit = document.createElement('button');
submit.type = 'submit';
const decoration = document.createElement('span');
decoration.textContent = ' →';
decoration.setAttribute('aria-hidden', 'true');
submit.append('Submit', decoration);
function clearError() {
input.removeAttribute('aria-invalid');
input.removeAttribute('aria-errormessage');
error.hidden = true;
}
input.addEventListener('input', () => {
if (!input.validity.valueMissing) clearError();
});
form.addEventListener('submit', (event) => {
event.preventDefault();
if (input.validity.valueMissing) {
error.hidden = false;
input.setAttribute('aria-invalid', 'true');
input.setAttribute('aria-errormessage', error.id);
error.focus();
} else {
clearError();
}
console.log(`aria-invalid=${input.getAttribute('aria-invalid')}; hidden=${error.hidden}; errorFocused=${document.activeElement === error}`);
});
form.append(error, label, ' ', submit);
document.body.append(form);
})();
</script>
</body>
</html>Result
aria-invalid=true; hidden=false; errorFocused=true aria-invalid=null; hidden=true; errorFocused=false
Save the example as `example.html` and open it in a browser. Open the developer console to see the logged lines. Click `Submit` with `Name` empty: the error appears and receives focus. On the page, press Tab, type `Ada` in `Name`, and click `Submit` again. The error clears as soon as you enter a value, and the form sends no data. The arrow remains visible and does not contribute to the button’s accessible name.
Related lessons
- JavaScript · Lesson 23 · Intermediate · Version: 3