CKTIS / JavaScript
Accessible interactions in JavaScript
An accessible interaction combines keyboard operation, predictable focus, and state that assistive technology can understand. JavaScript should synchronize visibility with ARIA and communicate updates without moving focus unnecessarily.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
element.setAttribute('aria-expanded', String(expanded));
panel.hidden = !expanded;
trigger.focus();
status.textContent = message;An accessible interaction lets people operate an interface without a mouse and understand its changes using assistive technology. Start with native buttons: they provide semantics and keyboard activation. ARIA describes the interface but does not add behavior itself. The ECMAScript specification describes the language and separates it from its host environment; the supplied source does not define DOM, HTML, or ARIA.
ARIA state and keyboard focus management means synchronizing state, visibility, and focus. In the example, aria-expanded matches the inverse of panel.hidden, while aria-controls identifies panel. In the opening test, focus is already on the Details button and stays there; the panel is not a modal dialog. The Close details handler focuses Details before hiding the panel. When Details closes the panel, setExpanded moves focus to Details before hiding the panel if focus is still inside it. The console test covers both closing paths, including activating Details without moving focus to it first. Do not remove the visible focus indicator or use positive tabindex values to repair navigation order.
ARIA live-region announcements communicate updates without moving focus. The example updates textContent in an existing region with role="status". That role implicitly provides aria-live="polite" behavior; the markup does not set an aria-live attribute. The aria-atomic="true" attribute indicates that the region's entire contents should be treated as the message. The region exists before the update. Do not use it instead of control labels or announce every minor change. Exact speech and announcement timing depend on the browser and assistive technology.
Save the example as an HTML file and check keyboard operation, attribute values, and screen-reader announcements. The expected output describes DOM state and where focus is, not what a screen reader is guaranteed to say. The example ran in an isolated Chromium environment, and its console output matched the expected output exactly. It uses ECMAScript 2015 syntax and browser interfaces; support in the target environment must be checked separately. Node 24 in the repository does not prove support for those interfaces.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Accessible disclosure</title>
</head>
<body>
<button id="toggle" type="button" aria-expanded="false" aria-controls="panel">Details</button>
<section id="panel" aria-label="Details" hidden>
<p>Additional information.</p>
<button id="close" type="button">Close details</button>
</section>
<p id="status" role="status" aria-atomic="true"></p>
<script>
const toggle = document.getElementById('toggle');
const panel = document.getElementById('panel');
const closeButton = document.getElementById('close');
const statusMessage = document.getElementById('status');
function setExpanded(expanded) {
if (!expanded && panel.contains(document.activeElement)) {
toggle.focus();
}
panel.hidden = !expanded;
toggle.setAttribute('aria-expanded', String(expanded));
statusMessage.textContent = expanded ? 'Details opened.' : 'Details closed.';
}
toggle.addEventListener('click', () => setExpanded(panel.hidden));
closeButton.addEventListener('click', () => {
toggle.focus();
setExpanded(false);
});
function report(step) {
console.log(`${step}: hidden=${panel.hidden}; aria-expanded=${toggle.getAttribute('aria-expanded')}; status=${JSON.stringify(statusMessage.textContent)}`);
}
report('Initial');
toggle.focus();
toggle.click();
report('Open');
console.log('Open kept focus on Details: ' + (document.activeElement === toggle));
// Test closing when activation does not first focus the close button.
toggle.blur();
console.log('Focus inside panel before close: ' + panel.contains(document.activeElement));
closeButton.click();
report('Close');
console.log('Focus returned to Details: ' + (document.activeElement === toggle));
// Test closing with Details while focus remains inside the panel.
toggle.click();
closeButton.focus();
console.log('Focus inside panel before closing with Details: ' + panel.contains(document.activeElement));
toggle.click();
report('Close with Details');
console.log('Focus returned to Details: ' + (document.activeElement === toggle));
</script>
</body>
</html>Result
Initial: hidden=true; aria-expanded=false; status="" Open: hidden=false; aria-expanded=true; status="Details opened." Open kept focus on Details: true Focus inside panel before close: false Close: hidden=true; aria-expanded=false; status="Details closed." Focus returned to Details: true Focus inside panel before closing with Details: true Close with Details: hidden=true; aria-expanded=false; status="Details closed." Focus returned to Details: true
The test programmatically activates `Details` while focus is on `Close details`. Focus should return to `Details` before the panel is hidden.