Movendo o foco após uma ação
Envie o foco para onde a pessoa precisará dele em seguida.
Movendo o foco após uma ação é uma aula grátis de Web Accessibility Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Web Accessibility Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Web Accessibility Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Focus Is the User's Cursor
For keyboard and screen reader users, focus is where they are on the page. After an action, you decide where that point lands next.
Why Move Focus at All
When a button reveals new content, focus often stays put and the user never learns anything changed. Moving focus brings them to the result.
The focus Method
You move focus in JavaScript by calling element.focus() on the target you want the user to land on next.
document.getElementById('results').focus();Make the Target Focusable First
A plain div will not accept focus, so give the destination tabindex="-1" so your script can move focus to it.
<div id="results" tabindex="-1">3 matches found</div>Example: Reveal and Focus
After loading new content, show it and then send focus there so the screen reader starts reading the new region. 🔎
panel.hidden = false;
panel.focus();Focus the Heading, Not the Whole Region
Pointing focus at the new section's heading often reads best, giving the user a clear title for where they just arrived.
Deleting an Item
When the user removes a row, focus would otherwise be lost. Move it to the next item or a nearby control so they can keep going.
Avoid the Focus-to-Top Jolt
Letting focus snap back to the page top after every action is jarring. Aim it at the relevant spot instead so context is preserved.
Do Not Steal Focus Randomly
Only move focus in response to a user action. Grabbing it during background updates yanks people out of whatever they were doing.
Confirm It Worked
Test by triggering the action with the keyboard and watching the focus ring land where you intended, every single time.
One Move Per Action
Each meaningful action should result in exactly one deliberate focus move. Predictable beats clever every time.
Quick Check
You want to move focus to a freshly shown div after a button click. What must the div have?
Recap
You learned to move focus on purpose: make the target focusable with tabindex="-1", call focus() after the action, and aim for the most useful spot. ✨
Perguntas Frequentes
A aula “Movendo o foco após uma ação” é grátis?
Sim — o texto completo de “Movendo o foco após uma ação” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Web Accessibility Academy, atualize para CoddyKit PRO. O curso de Web Accessibility Academy inclui 4 aulas no total.
O que vou aprender em “Movendo o foco após uma ação”?
Envie o foco para onde a pessoa precisará dele em seguida. Você pratica Web Accessibility Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Web Accessibility Academy?
Nenhuma experiência prévia é necessária. Web Accessibility Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.
Quanto tempo leva a aula “Movendo o foco após uma ação”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Web Accessibility Academy?
Sim. Cada aula de Web Accessibility Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- tabindex 0, -1 e nunca positivo
- Movendo o foco após uma ação
- Restaurando o foco quando uma camada é fechada
- Gerenciando tabindex móvel em componentes