> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/browser/event/default-action/hide-or-not-to-hide.md).

# hide or not to hide?

[browser](/web/browser.md) ⟩ [event](/web/browser/event.md) ⟩ [default action](/web/browser/event/default-action.md) ⟩ example ⟩ hide or not to hide?&#x20;

{% hint style="info" %}

```javascript
function
```

{% endhint %}

{% tabs %}
{% tab title="💈範例" %}

* :floppy\_disk: [replit](https://replit.com/@pegasusroe/epreventDefault#script.js)

```javascript
const {log} = console;

// ⭐️ import
import {$} from './js/Node+ext.js';
// -------------------------------------------

// elements
const rabbit = $('#rabbit');
const button = $('button');

// scenario A
// -------------------------------------------------------------
// 1. click the button
//
// 2. confirm dialog                          ("rabbit.onhide")
//    (confirm dialog shows BEFORE the log message, why❓)
//
// 3. click "OK"
//
// 4. dispatching "hide" event on rabbit ...  ("button.onclick")
//    (this message shows AFTER the confirm dialog, why❓)
//
// 5. default action prevented ...            ("rabbit.onhide")
// 6. "hide" event on rabbit dispatched ...   ("button.onclick")
// 7. "hiding rabbit" cancelled ...           ("button.onclick")
//
// scenario A
// -------------------------------------------------------------
// 1. click the button
// 2. confirm dialog                          ("rabbit.onhide")
// 3. click "Cancel"
// 4. dispatching "hide" event on rabbit ...  ("button.onclick")
// 5. "hide" event on rabbit dispatched ...   ("button.onclick")

// button.onclick
button.onclick = () => {
    
    // ⭐️ cancelable custom "hide" event
    let hideEvent = new CustomEvent("hide", {
      cancelable: true  // ⭐️ preventDefault doesn't work without this
    });

    // start to dispatch hide event
    log(`dispatching "hide" event on rabbit ...`);

    // ⭐️ dispatch 'hide' on rabbit and see if the event handler
    // decides to do the default actions or not.
    let doDefaults = rabbit.dispatchEvent(hideEvent);
    
    log(`"hide" event on rabbit dispatched ...`);

    if (!doDefaults) {
      log('"hiding rabbit" cancelled ...');
      return;
    }
      
    // hide the rabbit
    rabbit.hidden = true;
};

// rabbit.onhide
rabbit.addEventListener('hide', (event) => {
    if (confirm("Cancel hidding?")) {

      // By calling event.preventDefault(), an event handler 
      // may send a signal to cancel default actions.

      // ⭐️ In this case, elem.dispatchEvent(event) returns false❗️
      //    "Don't do default actions."
      event.preventDefault();

      log('default action prevented ...');
    }
});
```

{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="warning" %} <mark style="color:yellow;">**return value**</mark> of [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) ⟩ [.dispatchEvent(event)](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent)

* <mark style="color:red;">**false**</mark>&#x20;
  * if <mark style="color:blue;">`event`</mark> is <mark style="color:yellow;">**cancelable**</mark>, and&#x20;
  * <mark style="color:red;">**at least**</mark>**&#x20;**<mark style="color:yellow;">**one**</mark> of the event's handlers called [.preventDefault()](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault).&#x20;
* <mark style="color:green;">**true**</mark>, otherwise.
  {% endhint %}
  {% endtab %}

{% tab title="👥 相關" %}

* [event dispatching](/web/browser/event/dispatch.md)
* [capturing phase](/web/browser/event/propagation/capturing.md)
* the [return value](/web/browser/event/handler/return-value.md) of an [event handler](/web/browser/event/handler.md) is related to <mark style="color:yellow;">**canceling the default actions**</mark>.
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] JS.info ⟩ [event.preventDefault()](https://javascript.info/dispatch-events#event-preventdefault)
* [ ] :green\_book: [Full Stack open](https://fullstackopen.com/en/part0/fundamentals_of_web_apps#single-page-app)
  {% endtab %}

{% tab title="📘 手冊" %}

* [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) ⟩ [.dispatchEvent()](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent)
* [Event](/web/browser/event.md) ⟩ [.preventDefault()](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/browser/event/default-action/hide-or-not-to-hide.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
