CKTIS / JavaScript
Find an element by ID and change its `hidden` state
Look up an HTML element by its exact ID, handle a missing match, and use `hidden` to change the attribute without removing the element from the DOM.
Version: Current web platform; runtime support varies
Syntax
// These are web platform APIs, not ECMAScript 2024 features.
const element = document.getElementById(id);
if (element instanceof HTMLElement) {
element.hidden = true; // Set the hidden attribute.
element.hidden = false; // Remove the hidden attribute.
}
// Hidden Until Found depends on the browser's HTML implementation:
// element.hidden = "until-found";Document.getElementById/hidden describes two steps: find the element with document.getElementById(id), then work with its hidden property. Pass the ID itself, without the CSS selector prefix #; matching is case-sensitive. The method returns the first matching Element in document tree order, or null. IDs should be unique. An element not yet inserted into the document tree is not found, but a hidden element is.
Check the result before accessing .hidden: reading or assigning a property on null throws a TypeError. A match can also be a non-HTML element, such as SVG; instanceof HTMLElement checks the type within the same realm. These are web platform APIs, not ECMAScript 2024 features. The separate, read-only document.hidden property reports document visibility, not element visibility.
HTMLElement.hidden reflects the element's HTML hidden attribute. Assign true to set the attribute to an empty value or false to remove it. Reading gives false when the attribute is absent and true in the ordinary hidden state; even setAttribute("hidden", "false") hides the element. Where "until-found" is supported, you can assign and read that string to represent a state in which find-in-page or fragment navigation can reveal the content.
hidden neither removes the node nor protects sensitive data. Removing the attribute does not guarantee visibility: CSS or a hidden ancestor can still hide the element, and CSS display can also override ordinary hiding.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>getElementById and hidden</title>
</head>
<body>
<script>
(() => {
const doc = document.implementation.createHTMLDocument();
const panel = doc.createElement("section");
panel.id = "notice";
console.log(doc.getElementById("notice") === null);
doc.body.appendChild(panel);
const match = doc.getElementById("notice");
if (!(match instanceof HTMLElement)) {
throw new Error("Expected an HTML element with id notice");
}
match.hidden = true;
console.log(`${match.hidden} ${match.hasAttribute("hidden")} ${doc.getElementById("notice") === match}`);
match.hidden = false;
console.log(`${match.hidden} ${match.hasAttribute("hidden")}`);
})();
</script>
</body>
</html>Result
true true true true false false
Save the example as an HTML file, open it in a browser, and check the developer console. The separate document keeps the example isolated from the current page. The three lines show no match before insertion, that the same element is still found while hidden, and that assigning `false` removes the attribute.
Related lessons
- JavaScript · Lesson 35 · Advanced · Version: 4