Full answer
Accessible DOM scripting means that people who use a keyboard, a screen reader, or voice control can do everything a mouse user can do. The first rule of ARIA is to use a native element when one exists. A button is focusable, activates with Enter and Space, and has the role button. A div with a click handler has none of that.
Disclosure (show and hide): use a button with aria-expanded="true" or "false" and aria-controls pointing to the id of the panel. When toggled, update the attribute and show or hide the panel with the hidden attribute (which also removes it from the accessibility tree). Do not hide with opacity alone. Update the attribute in the same place as the visual change, so they cannot drift apart.
Announcing changes: content that changes without a page load is invisible to a screen reader unless announced. Use a live region: an element with role="status" (polite) or role="alert" (assertive) that exists in the page before you change its text. Put short messages in it, such as "3 results found" or "Item added". Do not move focus just to announce.
Forms: on an invalid field, set aria-invalid="true", connect the error text with aria-describedby, and put focus on the first invalid field after a failed submit. Use a label linked with for, not only a placeholder.
Focus management: when content opens (a dialog) move focus into it and return it to the trigger on close. The native dialog element with showModal() does this, plus a focus trap and the Escape key. When you remove the focused element, such as a deleted list item, move focus to a sensible neighbor, otherwise it falls back to the page start.
Also keep a visible focus style, never remove outlines without a replacement, and test by tabbing through the page. Automated tools catch only part of the problems, so a manual pass is needed.
document.body.innerHTML = `
<button id="toggle" aria-expanded="false" aria-controls="details">Show details</button>
<div id="details" hidden>More information</div>
<div id="status" role="status"></div>
<form id="signup" novalidate>
<label for="email">Email</label>
<input id="email" type="email" aria-describedby="email-error">
<div id="email-error"></div>
<button type="submit">Sign up</button>
</form>`;
const $ = (selector) => document.querySelector(selector);
// 1. Disclosure: keep aria-expanded and hidden in sync in one function
function setExpanded(button, panel, expanded) {
button.setAttribute('aria-expanded', String(expanded));
panel.hidden = !expanded;
button.textContent = expanded ? 'Hide details' : 'Show details';
}
$('#toggle').addEventListener('click', () => {
const open = $('#toggle').getAttribute('aria-expanded') === 'true';
setExpanded($('#toggle'), $('#details'), !open);
$('#status').textContent = open ? 'Details hidden' : 'Details shown';
});
const state = () => ({
expanded: $('#toggle').getAttribute('aria-expanded'),
panelHidden: $('#details').hidden,
label: $('#toggle').textContent,
announced: $('#status').textContent,
});
console.log('initial', state());
$('#toggle').click();
console.log('after click', state());
$('#toggle').click();
console.log('after second click', state());
// 2. Form errors: aria-invalid, aria-describedby text, focus on the first invalid field
$('#signup').addEventListener('submit', (event) => {
event.preventDefault();
const input = $('#email');
const valid = /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(input.value);
if (valid) input.removeAttribute('aria-invalid');
else input.setAttribute('aria-invalid', 'true');
$('#email-error').textContent = valid ? '' : 'Enter an email address such as name@example.com';
if (!valid) input.focus();
$('#status').textContent = valid ? 'Signed up' : 'The form has 1 error';
});
$('#signup').requestSubmit();
console.log('invalid:', $('#email').getAttribute('aria-invalid'), '| focused:', document.activeElement.id, '|', $('#email-error').textContent);
$('#email').value = 'asha@example.com';
$('#signup').requestSubmit();
console.log('valid:', $('#email').hasAttribute('aria-invalid'), '|', JSON.stringify($('#email-error').textContent), '|', $('#status').textContent);
// 3. Removing the focused element: move focus to a sensible place
document.body.insertAdjacentHTML('beforeend', '<ul id="list"><li><button>Delete A</button></li><li><button>Delete B</button></li></ul>');
const [first, second] = document.querySelectorAll('#list button');
first.focus();
console.log('before delete, focus on:', document.activeElement.textContent);
first.closest('li').remove();
console.log('after remove, focus on:', document.activeElement === document.body ? 'body (lost)' : document.activeElement.textContent);
second.focus();
console.log('after moving focus to the next item:', document.activeElement.textContent);Written for a browser console or script tag; checked here with Node.js v24.22.1 and jsdom, two runs with identical output. Output: initial { expanded: 'false', panelHidden: true, label: 'Show details', announced: '' } after click { expanded: 'true', panelHidden: false, label: 'Hide details', announced: 'Details shown' } after second click { expanded: 'false', panelHidden: true, label: 'Show details', announced: 'Details hidden' } invalid: true | focused: email | Enter an email address such as name@example.com valid: false | "" | Signed up before delete, focus on: Delete A after remove, focus on: body (lost) after moving focus to the next item: Delete B
Key points
- Prefer native elements; they give role, focus, and keyboard behavior for free.
- Disclosure: button with aria-expanded and aria-controls, panel shown with the hidden attribute.
- Announce changes with an existing live region (status or alert), without moving focus.
- Invalid fields: aria-invalid, aria-describedby for the message, focus on the first error.
- Move focus into opened content, return it on close, and never lose it when removing elements.
Common mistakes
- Using a div with a click handler instead of a button.
- Changing the visual state and forgetting to update aria-expanded.
- Creating the live region at the same time as the message, so it is not announced.
- Removing the focused element and leaving focus on the page body.
Follow-ups
- What is the difference between role="status" and role="alert"?
- How does the native dialog element help with focus?
- Which parts of accessibility testing can be automated, and which cannot?
Tips
- Start with native elements and say why.
- Mention keeping ARIA state in sync in one function.
- Describe focus handling for open, close, and delete actions.