# .hidePopover()

> The `.hidePopover()` JS method hides a popover element.

## How do I close a popover with JavaScript?

The method that hides a shown popover element by removing it from the top layer.

- Topic: Object on Page
- Type: method
- Browser support: Chrome 114+, Chrome Android 114+, Edge 114+, Firefox 125+, Firefox Android 125+, Safari 17+, Safari iOS 17+

## Example

Wrong:

```js
closeBtn.addEventListener('click', () => {
  menu.hidePopover();
  saveMenuState();
});
```

Right:

```js
closeBtn.addEventListener('click', () => menu.hidePopover());
menu.addEventListener('toggle', (e) => {
  if (e.newState === 'closed') saveMenuState();
});
```

The `toggle` event fires for Esc and outside clicks too, so cleanup runs however the popover closes.

## Common mistakes

- Running cleanup only in a close button handler, so it is skipped when the user presses Esc.

## Key points

- The popover is closed with `hidePopover()`, not by setting `display: none` or removing the `popover` attribute.
- Code that reacts to closing listens for the `toggle` event with `newState` 'closed', so Esc and outside clicks are handled too.
- The element has the `popover` attribute, otherwise the call throws.

## Interview questions

### How do I close a popover with a button without JavaScript?

Give the button `popovertarget` with the popover's `id` and `popovertargetaction='hide'`.

### Why does my popover close when I click outside it?

Popovers with `popover='auto'` close on outside clicks and Esc. Use `popover='manual'` to close them only from code or buttons.

## Related cards

- [.showPopover()](https://wonderium.dev/js/show-popover.md)
- [.togglePopover()](https://wonderium.dev/js/toggle-popover.md)
- [\[popover\]](https://wonderium.dev/html/popover.md)
- [\[popovertarget\]](https://wonderium.dev/html/popovertarget.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/hide-popover
- Last updated: 2026-11-25
