Skip to content
FunnelGuard

Fix guide by FunnelsGuard

Focus not moved into modal or returned on close: how to fix

PRF-315Medium impactOther toolBy FunnelGuardPublished Updated
On this page

What this issue is

A modal is a popup layer such as a lead form, a booking window, a cookie panel or an offer. Focus is the element that currently receives keyboard input. You can see it as the outline around a link or field when you press Tab.

This issue has two halves. On open, focus stays on the trigger button, or on the page underneath, instead of moving into the modal. On close, focus is not sent back to the trigger, so it resets to the top of the document or disappears. Both relate to WCAG 2.2 success criteria such as 2.4.3 Focus Order and 4.1.2 Name, Role, Value.

Why it costs you leads or sales

People who use a keyboard, a switch device or a screen reader cannot reach your form if focus stays behind the popup. They may press Tab many times through hidden page content before reaching the modal, or never reach it. A screen reader may not announce that a dialog opened, so the user does not know it exists.

On close, losing focus sends them back to the top of the page. They must tab through the header and menu again to find where they were. In a checkout or booking flow, many will give up.

Keyboard user opens a lead form modal
  1. Land on pageTabs to the Get a quote button
  2. Press EnterModal opens visually
  3. Focus stays behindNext Tab goes to page links, not the form
  4. Fill in formCannot reach the fields
  5. Lead submittedNever happens

Focus stays behind the modal, so the form is never reached.

Keyboard users who complete the modal formExample numbers
Keyboard users who complete the modal form (example)Before fix: 10 %; After fix: 70 %027541070Before fixAfter fix%

Example: illustrative numbers only, not measured data. Completion among keyboard-only visitors before and after fixing focus handling.

How to check if your site has it

You can test this in two minutes with only your keyboard. Do it on every modal that matters: lead forms, booking popups, quick-view windows, cart drawers and consent panels.

Manual keyboard test
  1. Tab to the trigger

    Press Tab until the button that opens the modal has a visible outline. Note which button it is.

  2. Open it with Enter or Space

    Do not use the mouse. The modal should appear.

  3. Check where focus went

    Focus should now be on the modal itself, its heading, or its first field or close button. If the outline is still on the trigger or has vanished, focus did not move in.

  4. Press Tab a few times

    Focus should cycle through elements inside the modal only. If it goes to page links behind it, the modal does not manage focus.

  5. Close with Escape

    Press Escape, or activate the close button. Focus should return to the original trigger button.

  6. Repeat with a screen reader

    Use NVDA or VoiceOver. On open it should announce the dialog and its name, for example the heading text.

For a faster check, open the browser console and run document.activeElement after the modal opens. It should return an element inside the modal.

Check where focus is (run in the console after opening the modal)javascript
console.log(document.activeElement);
console.log(document.activeElement.closest('[role="dialog"], dialog'));

How to fix it

The most reliable fix is the native HTML <dialog> element opened with showModal(). The browser moves focus into the dialog, keeps Tab inside it, closes it on Escape and returns focus to the element that was focused before. If you need a custom modal, do those jobs yourself.

Native dialog (recommended)html
<button id="open-quote">Get a quote</button>

<dialog id="quote-dialog" aria-labelledby="quote-title">
  <h2 id="quote-title">Get a quote</h2>
  <form method="dialog">
    <label for="q-email">Email</label>
    <input id="q-email" type="email" autocomplete="email">
    <button type="submit">Send</button>
    <button type="button" id="close-quote">Close</button>
  </form>
</dialog>

<script>
  const dlg = document.getElementById('quote-dialog');
  document.getElementById('open-quote').addEventListener('click', () => dlg.showModal());
  document.getElementById('close-quote').addEventListener('click', () => dlg.close());
</script>
Custom modal: move focus in, return it on closejavascript
let lastTrigger = null;
const modal = document.getElementById('my-modal'); // has role="dialog" aria-modal="true" aria-labelledby="..." tabindex="-1"

function openModal(trigger) {
  lastTrigger = trigger;
  modal.hidden = false;
  const first = modal.querySelector('input, select, textarea, button, a[href]');
  (first || modal).focus();
}

function closeModal() {
  modal.hidden = true;
  if (lastTrigger) lastTrigger.focus();
}

document.addEventListener('keydown', (e) => {
  if (e.key === 'Escape' && !modal.hidden) closeModal();
});
Fix by platform
  1. Identify the popup source

    Check whether the modal comes from your theme, a page builder (Elementor, Divi) or a popup plugin. Open the plugin's popup settings and look for accessibility or focus options.

  2. Update and test

    Update the plugin and theme, then repeat the keyboard test. If focus still does not move, switch to a plugin that supports focus management or replace the popup with a <dialog> in a Custom HTML block.

Modal behaviour

Before

  • Focus stays on the trigger button when the modal opens
  • Tab moves through the page behind the popup
  • Screen reader does not announce a dialog
  • After closing, focus resets to the top of the page

After

  • Focus moves to the first field or the modal heading
  • Tab cycles only through modal controls
  • Screen reader announces the dialog name
  • After closing, focus returns to the trigger button

How to confirm it's fixed

Confirmation checklist
  • Open the modal with Enter or Space: focus lands inside it
  • Tab and Shift+Tab stay within the modal
  • Escape closes the modal
  • Focus returns to the exact button that opened it
  • A screen reader announces the dialog and its name on open
  • The test passes on mobile with an external keyboard or screen reader
  • Every modal on the site has been tested, not just one

How to prevent it happening again

Modals get broken by theme updates, new popup plugins and marketing tools that add their own overlays. Build a short habit around it.

Prevention habits
  • Use native <dialog> for new modals
  • Add the keyboard test to your release checklist for any page with a popup
  • Retest after theme, plugin or popup tool updates
  • Run an automated accessibility scanner plus a manual keyboard pass before launching campaigns
  • Ask popup vendors whether their widget manages focus before buying

How FunnelGuard handles this

This issue is outside what FunnelGuard checks. FunnelGuard runs your customer journeys with a robot visitor and checks steps, tracking hits and test lead delivery. It does not judge keyboard focus behaviour or screen reader output. The kind of tool that fits is an accessibility testing tool: a browser-based accessibility checker for quick scans, combined with manual testing using only a keyboard and a screen reader such as NVDA or VoiceOver.

What FunnelGuard can help with is the neighbouring risk: whether your lead form, checkout or booking journey still completes on a schedule, and whether the lead reaches your inbox. You can start with the Free plan (1 site, 2 journeys, daily checks, no card) or a free read-only website audit.

Questions people ask

Why is focus not moving into my modal when it opens?

Most often the modal is shown by toggling a CSS class and nothing calls focus(). Focus therefore stays on the trigger button. Call focus() on the first field, the heading (with tabindex="-1") or the modal container after it appears, or use <dialog> with showModal().

How do I return focus to the button after closing a modal?

Save the trigger before opening, for example lastTrigger = document.activeElement. In your close function, hide the modal and call lastTrigger.focus(). A native <dialog> opened with showModal() does this automatically.

Where should focus go when a modal opens?

Move it to the first interactive element, usually the first form field, or to the modal heading or container if there is introductory text a user should hear first. For a destructive confirmation, focus the least risky button. Avoid leaving it on the page behind.

Does the HTML dialog element handle focus automatically?

Yes, when opened with showModal(). The browser moves focus into the dialog, keeps it there, makes the rest of the page inert, closes on Escape and restores focus to the previously focused element. Dialogs opened with show() do not make the page inert.

Is aria-modal enough to fix modal focus?

No. aria-modal="true" tells assistive technology the content behind is inactive, but it does not move focus or stop keyboard users tabbing out in every browser. You still need to manage focus with script or use <dialog>.

What is the difference between a focus trap and missing focus management?

A keyboard trap means users cannot leave the modal. Missing focus management means focus never enters the modal or does not return after it closes. A good modal keeps Tab inside while open but always allows Escape or a close button.

Can I test modal focus without a screen reader?

Yes. Use only your keyboard: Tab to the trigger, press Enter, check the focus outline is inside the modal, Tab around, press Escape, and check focus returns to the trigger. Adding a screen reader then confirms the dialog is announced.

Can FunnelGuard detect modal focus problems?

No. This is outside what FunnelGuard checks. Use an accessibility testing tool plus manual keyboard and screen reader testing. FunnelGuard instead monitors whether your funnels, tracking and test leads still work.

Does FunnelGuard catch this?

Other tool

Needs a different kind of tool.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.