Search topics
Keyboard traps in editors and form fields
A text editor takes Tab to indent, or an email field pulls focus back until the address is valid, so Tab and Shift + Tab can't leave.
event.preventDefault() The problem
Keyboard users who Tab into the editor, or into the email field before typing a valid address, cannot Tab out, so the rest of the page is out of reach
A form has a Notes editor followed by a Save note button. Tab into the editor and type. Press Tab to move on to Save note: the editor indents the line instead. Press Shift + Tab to go back: it indents again. Nothing on the keyboard moves focus out of the editor, so Save note and the rest of the page can't be reached.
The same site's newsletter sign-up has an Email address field followed by Subscribe. Tab into the field and press Tab again before typing: a message asks for an email address, and focus comes straight back to the field. Shift + Tab, to go back up the page, does the same. Type half an address, such as "name@", and the field still won't let go. Only an address that passes the check frees the keyboard, so someone who was only passing through the field on the way down the page can't get past it without typing one.
To support indenting, a script listens for keydown on the editor, cancels every Tab with preventDefault() and inserts spaces. It never checks Shift, and it offers no other key to leave. The email field's script means well: it checks the address on blur, shows the message, and calls focus() while the address is invalid, to keep the person on the field until it's right. By the time blur fires, the browser is already moving focus on, and the script moves it straight back, so Tab and Shift + Tab both end on the field again. Success criterion 2.1.2 allows a component to keep focus only when the user is told how to move it away. Neither does: the editor has no way out at all, and the field's message says what to type, not how to leave without typing it.
Anyone who uses a keyboard instead of a pointer is caught, power users included. A sighted mouse user can click past the editor and never notice. The email field pulls focus back after a click elsewhere too, so mouse users notice that one, but nothing stops them pointing at the rest of the page.
2 of 10 groups affected
- No vision (affected)
- Low vision (not affected)
- Colour vision (not affected)
- No hearing (not affected)
- Hard of hearing (not affected)
- No speech (not affected)
- Motor (affected)
- Reach, strength (not affected)
- Cognitive (not affected)
- Seizures (not affected)
- Limited manipulation People with motor disabilities, and switch and voice users whose software sends Tab, get stuck in the editor or the field. They have to reload the page and lose what they typed, or type an address that passes just to move on.
- Without vision Screen reader users move by keyboard too, so they are caught in the same trap. In the email field, each time focus comes back the field is announced again, and nothing says how to leave it.
The markup is a labelled textarea and a labelled input, so a static scan finds nothing wrong. Both traps live in scripts that only act when a key is pressed or focus leaves, and a probe has to press Tab from inside the component to see them. A Tab walk sees the empty email field hold focus, but a field that only objects once something is typed holds nobody during the walk, because the walk types nothing. AI coding tools can add Tab-to-indent because developers expect it in code editors, without the way out, and asked to keep the user on a field until it is valid, they can write the focus() call shown below. Traps also hide in third-party widgets, such as consent banners, chat windows and embedded maps, whose code nobody on the team reads.
Try it
Move through the page with the keyboard alone, and see whether you can always get out again
Broken 1: type a note. Can you reach Save note with Tab, and get back out with Shift + Tab?
Broken 2: Tab into Email address and press Tab again, then try once more after typing “name@”. Can you reach Subscribe, or get back out with Shift + Tab?
Apply a fix. Try the same keys again. Can you reach Save note or Subscribe with Tab, and get back out with Shift + Tab, even with the address left empty?
Broken Can you fix this?
The fix
Let keyboard focus leave the editor and the field
An editor that keeps every Tab
<label for="notes">Notes</label><textarea id="notes" name="notes"></textarea><button type="submit">Save note</button>What is wrong3 points
- Tab can't move on. Every Tab, with or without Shift, is cancelled and turned into two spaces.
- People are stuck in the editor. Focus can't leave it with the keyboard.
- People looking for another way out find no clue. The page says nothing about another key.
Tab moves focus, as the browser intends
<label for="notes">Notes</label><textarea id="notes" name="notes"></textarea><button type="submit">Save note</button> <label for="email">Email address</label><input id="email" name="email" type="email" required aria-describedby="email-error"><p id="email-error"></p><button type="submit">Subscribe</button>How it is fixed3 steps
- Remove the editor's keydown handler. A
textareaalready lets people type, and the browser moves focus out of it on Tab and Shift + Tab. - Remove the field's
focus()call. The field still checks the address when focus leaves it and shows the message, then lets focus go. - Keep
requiredandtype="email". They still stop the form from being sent with a wrong address: the browser moves focus to the field at that point, because the person asked to send.
This fixes both Befores, and both the editor and the field pass 2.1.2. It is the smallest change, and nobody has to learn a special key. If people need indentation, add an Indent button or indent automatically when they press Enter.
Why this fix: Use native HTML first
Both pass, and both treat the email field the same way: say what is wrong and let focus go. They differ only in the editor. Letting Tab move focus costs nothing and asks nothing of the user, so use it for notes, comments, messages and any field where indenting isn't central to the task. Keep Tab for indenting only in editors where people really write code or structured text, and then always pair it with the release key and its written instruction.
Escape often has another job, such as closing the dialog the editor sits in. If so, decide which wins, or pick a different release key and write that one down; some editors use Ctrl + M to switch Tab between indenting and moving focus. Checking a field when focus leaves it is fine, as long as focus is left where the person sent it. Many forms also wait until something has been typed, or until the form is sent, before flagging an empty field, so that tabbing past it on the way down doesn't raise an error; how to write the message and connect it to the field is covered in form errors that don't say how to fix them. A modal dialog is the one place where keeping focus inside is correct; the modal-focus-management guide covers it, and pop-ups that can't be closed with a keyboard covers a modal that keeps focus with no way out.
Verify the fix
5 checks, no mouse.
Test with: Keyboard, Screen reader
Record the OS, browser and assistive-technology versions, the build, the date, and the actual result of each step. Repeat after the editor, the form's validation or any third-party script changes.
How screen readers handle aria-describedby
Limits
This is a starting pattern, not a guarantee for every application. The snippet indents with two spaces and leaves out undo and outdenting. Rich-text and code editor libraries have their own Tab settings; check what yours does. An editor that can be left but isn't read with the label shown beside it is covered in rich text editors announced without their label.
A trap can also sit in a third-party widget, such as an embedded player, a map or a chat window, including one in shadow DOM or an iframe. The script isn't yours to change, but the page still fails 2.1.2, whoever wrote it. Look for it on the live page with the widget loaded, not in your own templates. Report it to the vendor with the steps to reproduce it and the criterion, give another way to do the same task beside it, such as a link or a phone number, and replace the widget if it isn't fixed. Test the complete journey with a keyboard and your target assistive technology.
This is a recreated teaching example, not a finding about a named client. The code is a starting pattern: test it in your own product. No completed assistive-technology test is claimed here.
EaseWeb service
Fixed it? Keep it from coming back.
I turn guides like this one into rules your coding agents follow and checks your CI runs. Then I retest by hand with a keyboard and screen readers, periodically.