How do you make an old dropdown menu script keyboard accessible?
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.
Cite
CodeLifter 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.- CodeLifter editorsStaffStaff, 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
onmouseoveron a top-level item, switched a hidden<div>tovisibility: visible, and hid it again ononmouseout. The trigger was often a link to#orjavascript: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
menurole, 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 addingrole="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 firesclickon Enter and Space without extra code. The APG example's attribute table listsaria-controlspointing at the list andaria-expandedset 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
focusouthandler 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 samearia-expandedvalue.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.
Should the parent item stay a link?
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>witharia-expandedandaria-controlsto open each submenu; do not open on Tab focus. - Keep links in nested lists inside
<nav aria-label="...">; skiprole="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.
- Use a
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.
1 answer
- 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-builtmenubar, 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."