# Toggle an element

Flip the hidden attribute.

Canonical: https://1loc.dev/dom/toggle-an-element/
Version: 1.0.0
Runtime: JavaScript · modern browsers (DOM)
Verification: Usage lines executed in headless Chromium against the fixture markup; no external services
Updated: 2026-10-28

## Implementation

```javascript
function toggle(element) { element.hidden = !element.hidden; }
```

## Usage

Markup:

```html
<nav id="menu">Menu</nav>
```

Script:

```javascript
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.

## 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 does not validate its arguments. Add checks where it receives untrusted input.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/toggle-an-element.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: islamozbek
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Toggles the hidden attribute; the original set display: block, which breaks flex and grid. Executed in Chromium. Original MDX retained as inert source evidence.
