To hide or show HTML elements cleanly using vanilla JavaScript, toggle a dedicated CSS class (such as .is-hidden) or the HTML hidden attribute (el.hidden = true / el.hidden = false) rather than scattering inline style.display strings across your scripts. Toggling a class or hidden keeps presentation rules in CSS, preserves DOM nodes and event listeners, and makes hiding/showing predictable and easy to maintain.


1. Toggling a CSS Class (Recommended for Clean Separation)

Define a visibility rule in your stylesheet—such as .is-hidden { display: none; }—and use element.classList.toggle() or classList.add() / classList.remove() in JavaScript:

// Show or hide an element cleanly by toggling a CSS class
function setElementHiddenByClass(el, isHidden) {
  if (!(el instanceof Element)) {
    throw new TypeError("Expected a DOM Element.");
  }

  el.classList.toggle("is-hidden", Boolean(isHidden));
  el.setAttribute("aria-hidden", String(Boolean(isHidden)));
}

// Example usage:
const drawer = document.querySelector("#filter-drawer");
setElementHiddenByClass(drawer, true);  // Adds "is-hidden"
setElementHiddenByClass(drawer, false); // Removes "is-hidden"
  • Centralizes layout rules in CSS: Keeping display: none or transition/visibility rules in stylesheets avoids hardcoding "block", "flex", or "grid" inside JS.
  • Boolean second argument on toggle(): Passing Boolean(isHidden) as the second argument to classList.toggle() forces a deterministic hidden or visible state without ambiguity.

2. Using the HTML hidden Property (el.hidden)

Every HTML element supports the boolean hidden DOM property, which maps to the hidden attribute and hides the element in standard browser stylesheets:

// Toggle native HTML hidden state cleanly
function setNativeHidden(el, shouldHide) {
  if (!(el instanceof HTMLElement)) {
    throw new TypeError("Expected an HTMLElement.");
  }

  el.hidden = Boolean(shouldHide);
}

// Example usage:
const helpText = document.querySelector("#password-hint");
setNativeHidden(helpText, true);  // Sets hidden attribute
setNativeHidden(helpText, false); // Removes hidden attribute
  • Built-in and concise: el.hidden = true / false works immediately without requiring extra JS setup.
  • Watch out for CSS display overrides: Custom CSS rules targeting the element with higher specificity or an explicit display property (such as display: flex) can override default [hidden] styling unless accounted for in CSS.

3. Saving and Restoring Inline style.display When Needed

If you must manipulate style.display directly—for instance, in a standalone snippet without external CSS—store any previous inline display value before hiding so restoring it does not clobber flex, grid, or inline-block layouts:

// Preserve previous inline display value when toggling style.display directly
const previousDisplayMap = new WeakMap();

function toggleInlineDisplay(el, show) {
  if (!(el instanceof HTMLElement)) {
    throw new TypeError("Expected an HTMLElement.");
  }

  if (show) {
    const restored = previousDisplayMap.get(el) ?? "";
    el.style.display = restored;
    previousDisplayMap.delete(el);
  } else {
    if (el.style.display && el.style.display !== "none") {
      previousDisplayMap.set(el, el.style.display);
    }
    el.style.display = "none";
  }
}

// Example usage:
const toolbar = document.querySelector("#selection-toolbar");
toggleInlineDisplay(toolbar, false); // Hide
toggleInlineDisplay(toolbar, true);  // Restore previous inline display
  • Avoid guessing "block": Forcing el.style.display = "block" when showing an element can break flex or grid containers—restoring "" lets CSS take over again.
  • Use WeakMap for metadata: Storing per-element state in a WeakMap avoids polluting DOM elements with custom properties and prevents memory leaks when nodes are removed.

TL;DR: Hide or show HTML elements cleanly in vanilla JavaScript by toggling a CSS class with el.classList.toggle("is-hidden", isHidden) or setting el.hidden = isHidden, falling back to saved inline style.display values only when standalone inline control is required.