How to Show and Hide an Element with the hidden Property in JavaScript
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
<element>.hidden = true;
<element>.hidden = false;
<element>.hidden = !<element>.hidden;
| Part | What it does |
|---|---|
<element> | The element to hide or show. |
true | Hides it. The browser adds the hidden attribute to the element. |
false | Shows it again, by removing the attribute. |
!<element>.hidden | The 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:

<!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:

Step 3: Click the Button
Click Show the answer:

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.hiddenwastrue, because the paragraph has thehiddenattribute 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
| Mistake | What happens | Do 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 rule | Don'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 true | Use the values true and false, without quotes |
| Expecting hidden elements to be gone | They're still in the page and in the code, just not shown | Remove the element if it must go away |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The element doesn't hide when hidden is true | A CSS rule sets its display | Remove that rule, or add [hidden] { display: none; } |
| The element never shows | Something sets hidden back, or you set it to 'false' in quotes | Set it to false, and check for other code that hides it |
Cannot read properties of null | The element wasn't found | Check 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
- HTMLElement: hidden property, on MDN.
- hidden global attribute, on MDN.
