Toggle an element

Flip the hidden attribute.

JavaScriptBrowser · DOM Version 1.0.0 · MIT · .md

toggle-an-element.js
function toggle(element) { element.hidden = !element.hidden; }

Usage

Markup

fixture.html
<nav id="menu">Menu</nav>

Script

usage.js
const menu = document.querySelector('#menu');
toggle(menu);
menu.hidden; // → true
toggle(menu);
menu.checkVisibility(); // → true

Lines marked → were executed in headless Chromium against the markup above, and each result was compared with the value shown. Lines marked e.g. are illustrative.

Know the limits

  • The original’s first version set display to block, which breaks flex and grid containers; toggling hidden restores the element’s own display.
  • A stylesheet display rule overrides hidden; see Hide an element. For a disclosure widget, <details> needs no JavaScript.

The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.

Source & maintenance

Based on Toggle an element from the original 1loc collection by Nguyen Huu Phuoc, with islamozbek. Copyright (c) 2020 phuoc-ng. MIT licensed.

Toggles the hidden attribute; the original set display: block, which breaks flex and grid. Executed in Chromium. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Usage lines executed in headless Chromium against the fixture markup; no external services · reviewed October 28, 2026.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.