Form Submission and Error Focus
What you'll learn: How to guide keyboard users and screen reader users to the right place after submitting a form.
When a user submits a form, something important happens next—either success or errors. But where should their attention go? For keyboard and screen reader users, you must programmatically move focus to help them understand what happened, rather than leaving them stranded at the submit button.
Why This Matters
Imagine filling out a long form, pressing submit, and hearing… nothing. No confirmation, no error message, just silence. You'd have to hunt through the entire page to figure out what went wrong. For screen reader users, this is the reality when developers don't manage focus properly.
The Two Post-Submission Scenarios
Success: Move focus to a success message or confirmation heading. This immediately announces the good news.
Validation Errors: Move focus to either:
- The first field with an error, so users can start fixing issues immediately
- An error summary container at the top of the form that lists all problems
Typical Implementation Pattern
- Prevent form's default submission
- Validate the form data
- If errors exist, focus the first error field or error summary
- If successful, focus a success message element (often with
tabindex="-1"since headings aren't naturally focusable) - Announce changes to screen readers using ARIA live regions if needed
Think of it like a helpful guide in a museum—after you complete an activity (submit the form), the guide immediately walks you to the next relevant spot rather than leaving you to wander aimlessly.
Key Takeaway: Always move focus after form submission—to the first error field when validation fails, or to a success message when it succeeds, ensuring keyboard and screen reader users immediately understand the outcome.