Skip to main content

How to Show and Hide an Element with the hidden Property in JavaScript

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Every element has a hidden property. Set it to true and the element disappears from the page; set it to false and it comes back. It's the JavaScript side of the HTML hidden attribute, so you can start an element hidden in HTML and show it from a script.

Syntax​

Syntax
<element>.hidden = true;
<element>.hidden = false;
<element>.hidden = !<element>.hidden;
PartWhat it does
<element>The element to hide or show.
trueHides it. The browser adds the hidden attribute to the element.
falseShows it again, by removing the attribute.
!<element>.hiddenThe opposite of its current state, so one line toggles it. ! turns true into false and back.

MDN's HTMLElement: hidden property page describes the property and the attribute it reflects.

The Example​

The example page has a button and an answer that starts hidden. Each click of the button toggles the answer: the first click shows it, the next hides it again.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, a page with a button Show the answer and a hidden paragraph The answer is 42, and a script with a toggle function that sets answer.hidden to not answer.hidden, connected to the button's click event.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>hidden</title>
</head>
<body>
<button type="button" id="toggle">Show the answer</button>
<p id="answer" hidden>The answer is 42.</p>

<script>
const answer = document.getElementById('answer');

function toggle() {
answer.hidden = !answer.hidden;
}

document.getElementById('toggle').addEventListener('click', toggle);
</script>
</body>
</html>

The hidden attribute in the HTML starts the paragraph hidden. addEventListener('click', toggle) runs toggle each time the button is clicked.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser, with only the button showing:

The page in Chromium showing only a Show the answer button.

Step 3: Click the Button​

Click Show the answer:

The page in Chromium after the click: the Show the answer button, and below it the paragraph The answer is 42.

Click it again and the answer disappears. The screenshots come from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • At first, answer.hidden was true, because the paragraph has the hidden attribute in the HTML.
  • The first click set it to false, and the paragraph appeared.
  • The second click set it back to true. The same line, answer.hidden = !answer.hidden, flips it each time.

Common Mistakes​

MistakeWhat happensDo this instead
A CSS rule that sets display on the element, such as p { display: block; }The element stays visible although hidden is true: the CSS rule wins over the browser's own hiding ruleDon't set display on elements you hide, or add [hidden] { display: none; } to your CSS
Setting it to the string 'false'The element stays hidden: hidden became true, because any non-empty string counts as trueUse the values true and false, without quotes
Expecting hidden elements to be goneThey're still in the page and in the code, just not shownRemove the element if it must go away

Troubleshooting​

SymptomLikely causeFix
The element doesn't hide when hidden is trueA CSS rule sets its displayRemove that rule, or add [hidden] { display: none; }
The element never showsSomething sets hidden back, or you set it to 'false' in quotesSet it to false, and check for other code that hides it
Cannot read properties of nullThe element wasn't foundCheck the id, and put the script after the element

FAQ​

What's the difference between hidden and display: none?​

For the browser, an element with hidden gets display: none from the browser's own stylesheet, so both look the same. The difference is where you set it: hidden is part of the HTML element, while display is CSS, and a CSS rule with display overrides the browser's rule for hidden.

Do hidden elements still take up space?​

No. A hidden element is removed from the layout, so the elements below it move up. To keep its space, use the CSS visibility: hidden instead.

Can screen readers read hidden elements?​

No. Elements with hidden aren't shown to anyone, including people who use screen readers.

Conclusion​

element.hidden = true hides an element, false shows it, and element.hidden = !element.hidden toggles it. If it won't hide, look for a CSS rule that sets display on it.

Additional Resources​