Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLElement.hidden and click(): hiding an element vs. triggering a click

`hidden` reflects an HTML attribute, while `click()` dispatches a synthetic click. Hiding an element does not disable it or prevent a programmatic click.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

element.hidden
 element.hidden = true       // Set the hidden attribute
 element.hidden = false      // Remove the hidden attribute
 element.hidden = "until-found"
 element.click()             // No arguments; returns undefined

HTMLElement.hidden/click refers to two separate members. The read/write hidden property reflects the HTML hidden attribute. Reading it returns false if the attribute is absent, "until-found" in the hidden-until-found state, and true otherwise. Assigning true sets an empty attribute; assigning false removes it. The string "false" is not the boolean false: it leaves the element in the hidden state.

In browsers that support "until-found", setting it lets find-in-page or fragment navigation reveal the content. hidden reports the attribute state, not actual on-screen visibility: CSS can affect whether an element is displayed. Hiding an element neither removes it from the DOM nor protects its contents.

element.click() takes no arguments and returns undefined. It synchronously dispatches a synthetic click on the element with event.isTrusted === false. It does nothing on a disabled form control or when a click() call is already in progress on the same element. Being hidden does not by itself block the method.

A synthetic click can run an element's default action, such as toggling a checkbox, but it does not itself grant the user activation some APIs require. hidden and click() are web platform APIs, not ECMAScript 2024 features; they require an environment that provides the HTML DOM.

Examples

<!doctype html>
<meta charset="utf-8">
<title>HTMLElement.hidden and click()</title>
<script>
  const panel = document.createElement("section");
  const button = document.createElement("button");
  button.type = "button";
  panel.hidden = true;
  button.hidden = true;

  button.addEventListener("click", (event) => {
    console.log(`click: isTrusted=${event.isTrusted}`);
    panel.hidden = false;
  });

  console.log(`before: panel.hidden=${panel.hidden}`);
  button.click();
  button.disabled = true;
  button.click();
  console.log(`after: panel.hidden=${panel.hidden}`);
</script>

Result

before: panel.hidden=true
click: isTrusted=false
after: panel.hidden=false

Save the complete example as an HTML file and open it in a browser. The output appears in the developer console. Neither element is inserted into the page: the example shows attribute state and event behavior, not rendering. The first click removes `hidden` from the panel; the second does not run the listener.

Related lessons

  • JavaScript · Lesson 41 · Intermediate · Version: 3

Sources