For a button-like control with normal, hover, and clicked appearances, define the appearances in CSS and use JavaScript to toggle a class when it is clicked. If that control opens a menu panel, update the panel and the control’s state together. You usually do not need to swap image files to represent these states.
How should the three visual states work?
Keep presentation in CSS and interaction in JavaScript. The normal appearance is the default style; :hover handles pointer hover; and a class such as .is-active represents a selected or open control. A participant in the original May 2010 SitePoint discussion recommended this separation for the two menu links in the question.
.menu-toggle:hover {
/* Hover appearance */
}
.menu-toggle.is-active {
/* Selected or open appearance */
}
CSS can style the control with colors, borders, or a background image. Use an actual image element when the image itself is meaningful content; for a button’s decorative state, CSS is generally simpler than replacing image elements.
How can a click update the control and its panel?
Use a real <button> for an action that changes the page interface. Connect it to its panel with aria-controls, and expose whether the panel is open with aria-expanded. The following is an illustrative pattern, not a reproduction of the 2010 code:
Recommended Free Tools
#1 Best Overall
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
<button class="menu-toggle" aria-expanded="false" aria-controls="menu-one">
Menu 1
</button>
<div id="menu-one" hidden>…</div>
const button = document.querySelector(".menu-toggle");
const panel = document.getElementById("menu-one");
button.addEventListener("click", () => {
const isOpen = button.getAttribute("aria-expanded") === "true";
button.setAttribute("aria-expanded", String(!isOpen));
button.classList.toggle("is-active", !isOpen);
panel.hidden = isOpen;
});
The event listener flips the open state: when open, the next click closes the panel; when closed, it opens it. The class, accessibility state, and panel visibility all follow that same decision. Registering the handler with addEventListener() keeps behavior in JavaScript rather than embedding an inline onclick attribute in the markup; MDN documents the event-listener method and the button element.
Should multiple menu panels stay open?
Choose the behavior deliberately. For independent toggles, each button can open or close its own panel without affecting others. For an accordion that allows only one panel open at a time, opening a new panel should close the previously open one and reset its button’s aria-expanded value and active class. The original SitePoint script attempted to track the previously expanded panel, but it also declared showHide twice; the later declaration replaces the earlier one.
Whichever model you choose, keyboard users must be able to operate the controls, and the expanded state must stay in sync with the visible panel. A decorative image alone does not provide the semantics or keyboard behavior of an interactive control.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.When would swapping images make sense?
Image swapping is an option when the control genuinely needs distinct image assets, but it is not necessary just to show hover and selected states. A CSS class can select the appropriate background or other styling while leaving the interaction logic unchanged. For further context on image toggling and event handling, see the later Code Review example.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Quick Recap
Best Value
- JavaScript Jquery
- Introduces core programming concepts in JavaScript and jQuery
- Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
Rank #4
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Rank #3
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




