Sub topic 6
Making forms and pop-ups accessible
This chapter assumes you can write basic HTML and a little JavaScript, and that you have built the sign-up form and the <dialog> pop-up from the earlier chapters. Here you will check them against what keyboard and screen reader users need.
Why it matters for list growth
A visitor who cannot operate your form cannot subscribe. Keyboard-only users, screen reader users, and people with low vision or motor impairments make up a real share of any audience. Accessible forms also tend to convert better for everyone, because labels and clear errors reduce mistakes.
Labels and input hints
Every field needs a visible <label> tied to the input with matching for and id values. A placeholder is not a label. It disappears as soon as the visitor types, and many screen readers do not announce it reliably.
Add type="email" so mobile devices show the right keyboard. Add autocomplete="email" so browsers and assistive tools can fill the field. Both are already in the form you built.
Error messages that people can perceive
Browser-default validation bubbles vary between browsers. If you write your own messages, connect them to the field and announce them.
<form action="https://example.com/subscribe" method="post" novalidate id="signup">
<label for="email">Email address</label>
<input type="email" id="email" name="email" autocomplete="email"
required aria-describedby="email-error">
<p id="email-error" role="alert"></p>
<button type="submit">Subscribe</button>
</form>
<script>
const form = document.querySelector('#signup');
const input = document.querySelector('#email');
const error = document.querySelector('#email-error');
form.addEventListener('submit', (event) => {
if (!input.validity.valid) {
event.preventDefault();
error.textContent = 'Enter an email address such as [email protected].';
input.setAttribute('aria-invalid', 'true');
input.focus();
} else {
error.textContent = '';
input.removeAttribute('aria-invalid');
}
});
</script>Three things make this work. aria-describedby links the message to the input, so it is read with the field. role="alert" makes screen readers announce the text when it changes. Moving focus to the invalid field puts the visitor where they need to act. Say what is wrong and how to fix it. Do not rely on red text alone, because color-blind users may miss it.
Pop-ups: focus is the hard part
A pop-up that appears without managing focus can trap or lose keyboard users. The native <dialog> element opened with showModal() handles most of this for you:
- Focus moves into the dialog when it opens.
- Tab stays inside the dialog while it is open.
- Escape closes it.
- Focus returns to the previously focused element on close.
- The page behind becomes inert.
Give the dialog an accessible name with aria-labelledby, as in the earlier example. Always include a visible close button, such as the "No thanks" button in the method="dialog" form. Do not hide the close control or make it tiny, since it must be reachable by keyboard and large enough to tap.
Hidden honeypot fields
The honeypot field from the form chapter is hidden from sighted users. It must also be hidden from assistive tools, or a screen reader user may fill it in and be rejected as a bot. That is why the wrapper uses aria-hidden="true" and the input uses tabindex="-1". Test with a keyboard to confirm that Tab skips it.
Quick test checklist
- Complete the form using only Tab, Shift+Tab, Enter, and Space.
- Zoom the page to 200% and check that nothing overlaps or gets cut off.
- Turn on a screen reader and confirm labels, errors, and the dialog title are announced.
- Check that text and button colors have enough contrast (4.5:1 for normal text).
Exercise
A visitor using a screen reader submits your form with an empty email field. The page shows a red message under the field, but the screen reader says nothing. Name two changes that fix this.
Show answer
Link the message to the input with aria-describedby and mark the message element with role="alert" (or otherwise announce it when it changes). Also move focus to the invalid field and set aria-invalid="true". Writing the message in text instead of relying only on red color helps as well.