Hide an element
Set the hidden attribute.
function hide(element) { element.hidden = true; }
Usage
Markup
<p id="notice">Saved</p>
Script
const notice = document.querySelector('#notice');
hide(notice);
notice.checkVisibility(); // → false
Know the limits
- hidden removes the element from layout and from the accessibility tree. A stylesheet rule such as display: flex overrides it; add [hidden] { display: none !important } to be safe.
- visibility: hidden keeps the space. To hide content visually but keep it for screen readers, use a visually-hidden class instead.
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 Hide an element from the original 1loc collection by Nguyen Huu Phuoc, with iamdileepkumar, islamozbek. Copyright (c) 2020 phuoc-ng. MIT licensed.
Uses the hidden attribute; display and visibility trade-offs documented. Executed in Chromium. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Check if a given DOMRect is contained within another DOMRect — True when the target lies inside the container or on its edges.
- Check if two DOMRects intersect — Two rects intersect when they meet on both axes.
- Compare two DOMRects for size and position match — Compare x, y, width and height exactly.