October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Image Toggle on Click: Use CSS States and JavaScript Classes

Use CSS for normal, hover, and selected appearances, then let a JavaScript click handler toggle a class and the associated menu panel together.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • 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.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

SaleBestseller No. 1
Web Design with HTML, CSS, JavaScript and jQuery Set
Web Design with HTML, CSS, JavaScript and jQuery Set
Brand: Wiley; Set of 2 Volumes
$35.05
SaleBestseller No. 4
HTML and CSS: Design and Build Websites
HTML and CSS: Design and Build Websites
HTML CSS Design and Build Web Sites; Comes with secure packaging; It can be a gift option
$14.94
SaleBestseller No. 5
JavaScript and jQuery: Interactive Front-End Web Development
JavaScript and jQuery: Interactive Front-End Web Development
JavaScript Jquery; Introduces core programming concepts in JavaScript and jQuery; Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
$22.75
Best Value
Sale
JavaScript and jQuery: Interactive Front-End Web Development
  • JavaScript Jquery
  • Introduces core programming concepts in JavaScript and jQuery
  • Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
Rank #4
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.