Skip to content
CodeLifter
Search/ Browse prompts
CodeLifter Forum

How do you make an old dropdown menu script keyboard accessible?

Staff answerWritten by CodeLifter editors · Script Shelf1 replyupdated

Short answer

Replace the old hover-only trigger with a real <button> that toggles the submenu, set aria-expanded on that button to match the state, keep the links in a nested list inside a <nav>, close the submenu on Escape and when focus leaves, and stop using the ARIA menu role for ordinary site navigation.

Written by CodeLifter editors · Staff, CodeLifter ·
CiteCodeLifter Forum, "How do you make an old dropdown menu script keyboard accessible?", https://codelifter.com/forum/posts/how-do-you-make-an-old-dropdown-menu-script-keyboard-accessible/, accessed 2026-12-04.
  1. Staff, CodeLifter ·

    Why do old dropdown scripts fail for keyboard users?

    The classic cut-and-paste dropdown from the early 2000s did three things: it listened for onmouseover on a top-level item, switched a hidden <div> to visibility: visible, and hid it again on onmouseout. The trigger was often a link to # or javascript:void(0). Nothing in that recipe reacts to focus or to a key press, so someone moving through the page with Tab never sees the submenu at all, and a screen reader is never told that a submenu exists.

    Some scripts went the other way and opened every submenu on focus. W3C's fly-out menus tutorial explains why that is no better: submenus "should not open when using the tab key to navigate through the menu, as keyboard users would then have to step through all submenu items to get to the next top-level item."

    What pattern should replace it?

    The W3C ARIA Authoring Practices Guide has a working disclosure navigation menu example for exactly this case. Each section of the site gets a disclosure button, and pressing it shows or hides a nested list of links. The guide is explicit that it does not use the ARIA menu role, because site navigation "does not provide the complex functionality that assistive technologies expect in a widget that has the menu role." Many old scripts were "upgraded" by adding role="menu", which promises arrow-key behaviour the script never delivers.

    Start with the markup:

    <nav aria-label="Main">
      <ul>
        <li><a href="/">Home</a></li>
        <li>
          <button type="button" aria-expanded="false" aria-controls="sub-scripts">Scripts</button>
          <ul id="sub-scripts" hidden>
            <li><a href="/scripts/menus/">Menus</a></li>
            <li><a href="/scripts/forms/">Forms</a></li>
          </ul>
        </li>
      </ul>
    </nav>
    

    A <button> is focusable and fires click on Enter and Space without extra code. The APG example's attribute table lists aria-controls pointing at the list and aria-expanded set to "false" when it is hidden and "true" when it is visible.

    What script does it need?

    Much less than the original, in most cases:

    const nav = document.querySelector('nav[aria-label="Main"]');
    const buttons = nav.querySelectorAll("button[aria-expanded]");
    
    function setOpen(button, open) {
      button.setAttribute("aria-expanded", String(open));
      document.getElementById(button.getAttribute("aria-controls")).hidden = !open;
    }
    
    buttons.forEach((button) => {
      button.addEventListener("click", () => {
        const open = button.getAttribute("aria-expanded") !== "true";
        buttons.forEach((other) => setOpen(other, false));
        setOpen(button, open);
      });
    });
    
    nav.addEventListener("keydown", (event) => {
      if (event.key !== "Escape") return;
      const open = nav.querySelector('button[aria-expanded="true"]');
      if (open) {
        setOpen(open, false);
        open.focus();
      }
    });
    
    nav.addEventListener("focusout", (event) => {
      if (!nav.contains(event.relatedTarget)) {
        buttons.forEach((button) => setOpen(button, false));
      }
    });
    

    This matches the keyboard table in the APG example: Tab and Shift+Tab move among buttons and into an open list, Space or Enter toggles a button, and Escape "closes it and sets focus on the button that controls that dropdown." The focusout handler covers the guide's note that "Moving focus out of the navigation region also closes an open dropdown."

    Style the open state from the attribute, as the APG example does with CSS attribute selectors, so the visual and announced states cannot drift apart:

    nav button[aria-expanded="true"]::after { transform: rotate(180deg); }
    

    Why is the Escape key required?

    It is not only good manners. WCAG 2.1 success criterion 1.4.13, Content on Hover or Focus, applies to "sub-menus, and other nonmodal popups that display on hover and focus." The Understanding 1.4.13 page sets three conditions: the content must be dismissible without moving the pointer or focus, hoverable so the pointer can move onto it, and persistent until the user moves away or dismisses it. The APG example says its Escape behaviour is "necessary" to meet that criterion.

    Can you keep the hover behaviour?

    Yes, as long as it is an extra and not the only way in. The WAI tutorial adds a short timer before closing on mouseout, "a delay of one second", so that imprecise mouse movement does not snap the menu shut, and that also helps with the hoverable condition above. Keep the button as the keyboard and touch trigger, and make sure hover and click update the same aria-expanded value.

    The tutorial also warns that people with tremors may find fly-outs hard or impossible, and recommends repeating submenu links on the parent section's page. That is cheap insurance for any menu.

    If the top-level item went to a real page, keep that link and add a separate toggle button next to it, which is the "use button as toggle" approach in the WAI tutorial; the tutorial suggests including the parent's name in the button's hidden label, such as "show Space Bears submenu". The APG example page also links a variant, "Disclosure Navigation Menu with Top-Level Links", for that layout. If the parent only grouped links, turn it into the button.

    For other parts of an old script that need updating, the modernising thread covers moving inline handlers into a file, and the Script Shelf lists which classic menu scripts were rewritten.

    Short version

    • Use a <button> with aria-expanded and aria-controls to open each submenu; do not open on Tab focus.
    • Keep links in nested lists inside <nav aria-label="...">; skip role="menu" for site navigation.
    • Close on Escape (returning focus to the button) and when focus leaves the nav.
    • Hover can stay as an extra, with a short close delay, but never as the only trigger.
    1
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

  1. CodeLifter editorsStaffStaff, CodeLifter ·

    Should the arrow keys move between items too? They can, but they are optional for navigation menus, and Tab must keep working. The APG disclosure navigation example has a checkbox to switch arrow-key, Home and End support on, and explains why it is optional: the support "is primarily for the benefit of keyboard users who are not running a screen reader," because screen readers in reading mode "intercept these navigation keys and do not pass them to the page."

    It also says the arrow keys "supplement, but do not replace, tabbing among buttons and links," since the links are not inside a widget role that would take a single Tab stop. So if you add them, do not set tabindex="-1" on the links to force arrow-only movement. That is the step that turns a disclosure menu into a half-built menubar, which the APG covers as a separate, much more demanding pattern.

    A sensible order for an old site is: button toggles first, then Escape and focus-out closing, then test with a keyboard and a screen reader, and only then consider arrow keys. The APG's own notice says testing "with assistive technologies is essential before considering use in production systems."

CodeLifter Forum. Questions and answers on vibe coding prompts, AI-generated code provenance, project AI policies and MCP tools.