
Ensuring accessibility when altering the DOM with JavaScript involves several best practices. Here are some key strategies to maintain or enhance accessibility:
- Maintain Focus Order:
- When dynamically adding or removing elements, ensure the logical focus order is preserved.
- Use JavaScript to set focus to new interactive elements using the
focus()
method. - Ensure that newly added elements can be navigated using the keyboard.
const newButton = document.createElement('button'); newButton.textContent = 'New Button'; document.body.appendChild(newButton); newButton.focus(); // Moves focus to the new button
- Use ARIA Roles and Properties:
- Use ARIA (Accessible Rich Internet Applications) attributes to communicate the role, state, and properties of dynamic elements to assistive technologies.
- Ensure that the ARIA roles and properties are updated as the DOM changes.
const alertDiv = document.createElement('div'); alertDiv.setAttribute('role', 'alert'); alertDiv.textContent = 'This is an important message'; document.body.appendChild(alertDiv);
- Announce Changes to Screen Readers:
- Use live regions (e.g.,
aria-live
) to announce updates to the DOM. - Set
aria-live
topolite
for non-urgent updates andassertive
for urgent updates.<div id="live-region" aria-live="polite"></div>
const liveRegion = document.getElementById('live-region'); liveRegion.textContent = 'New content added';
- Preserve Keyboard Accessibility:
- Ensure that all interactive elements can be accessed and operated using a keyboard.
- Avoid using
tabindex
values greater than 0, which can disrupt the natural tab order.<button id="dynamic-button">Click me</button>
const button = document.getElementById('dynamic-button'); button.addEventListener('click', () => { // Do something });
- Provide Text Alternatives:
- Ensure all dynamic content has text alternatives for screen readers.
- Use
aria-label
oraria-labelledby
for interactive elements that don’t have visible text.const iconButton = document.createElement('button'); iconButton.setAttribute('aria-label', 'Close'); iconButton.innerHTML = '<svg>...</svg>'; // SVG icon without text document.body.appendChild(iconButton);
- Handle Visibility Changes:
- Ensure elements that are shown or hidden dynamically are properly handled for accessibility.
- Use
aria-hidden="true"
to hide elements from screen readers and update or remove the attribute when making them visible.const modal = document.getElementById('modal'); modal.setAttribute('aria-hidden', 'false'); // Show modal to screen readers modal.style.display = 'block'; // Make modal visible
- Announce Page Updates:
- Use announcements to inform users about significant changes like navigation updates or content loads.
const mainContent = document.getElementById('main-content'); mainContent.innerHTML = 'New content loaded'; mainContent.setAttribute('aria-live', 'assertive');
By following these practices, you can ensure that your dynamic web content remains accessible to all users, including those relying on assistive technologies.