Professional Business Website · Project

Professional Business Website · Phase 4 · Lesson 1 of 3

JavaScript Mobile Menu with Progressive Enhancement

Write a small script that collapses the navigation into a menu button on phones, accessible by keyboard and screen readers, and still usable if the script fails.

What you need

A computer (Windows, macOS or Linux) with Visual Studio Code (free) and a current web browser such as Chrome or Firefox. A smartphone is used for testing.

In this lesson you will

  • Toggle a navigation menu with JavaScript and aria-expanded
  • Apply progressive enhancement

Key concepts

event listenerclassListaria-expandedprogressive enhancementdefer

The lesson

Progressive enhancement means the page works without JavaScript and the script only improves it. Here, the navigation links are visible by default; the script hides them behind a menu button on small screens.

Use a real button element for the toggle, update aria-expanded to true or false, and toggle a CSS class. Load the script with the defer attribute so it runs after the HTML is read.

Keep scripts small. A business site rarely needs a framework, and every extra kilobyte costs customers data.

Step by step

  1. Add a button with aria-controls and aria-expanded to the header.
  2. Create js/menu.js that toggles a class and aria-expanded on click.
  3. Write CSS that hides the links only when the script has added a class to the page.
  4. Test with the mouse, the keyboard (Tab, Enter) and with JavaScript disabled.

Practice activity

Implement the mobile menu, test it with keyboard and screen reader basics, and confirm the site still works with JavaScript disabled.

Expected result: A menu button that works by touch and keyboard, announces its state, and a site that still shows its links without JavaScript.

Common mistakes

  • Using a div with a click handler instead of a button, which keyboards cannot reach
  • Hiding the menu with CSS so that it never appears when JavaScript fails

Check your understanding

  1. What does aria-expanded tell assistive technology?

    Show answer

    Whether the controlled menu is currently open or closed.

  2. What is progressive enhancement?

    Show answer

    Building a page that works without JavaScript and using scripts only to improve it.

Further learning

Projects are instructor-guided: an instructor sets milestones, checks your work and gives feedback, online or in person. Self-study of the public lessons does not lead to a certificate. Enrolment and fees follow the Academy's existing policies — contact the Academy.