# .showPopover()

> The `.showPopover()` JS method shows a popover element.

## How do I open a popover with JavaScript?

The method that shows an element with the `popover` attribute by adding it to the top layer. Throws an error if the element is not a popover.

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

```html
<div id="menu" popover>Menu</div>
<button onclick="document.getElementById('menu').showPopover()">Open</button>
```

Right:

```html
<div id="menu" popover>Menu</div>
<button popovertarget="menu" popovertargetaction="show">Open</button>
```

`popovertarget` opens the popover without script and exposes the expanded state to assistive tech.

## Common mistakes

- Calling `showPopover()` on an element without the `popover` attribute.

## Key points

- A button that only opens the popover uses `popovertarget`, not a click handler with `showPopover()`.
- The element has the `popover` attribute and is in the document, otherwise the call throws.
- When opened from JS, the trigger's `aria-expanded` is kept in sync in the `toggle` event.

## Interview questions

### Why does showPopover throw an 'InvalidStateError'?

Usually the element has no `popover` attribute or is not in the document. Check both before calling it.

### Does showPopover put the element above everything else?

Yes. An open popover renders in the top layer, above other content, regardless of `z-index`.

### How do I style an open popover?

Use the `:popover-open` pseudo-class, and `::backdrop` for the area behind it.

## Related cards

- [.hidePopover()](https://wonderium.dev/js/hide-popover.md)
- [.togglePopover()](https://wonderium.dev/js/toggle-popover.md)
- [\[popover\]](https://wonderium.dev/html/popover.md)
- [\[popovertarget\]](https://wonderium.dev/html/popovertarget.md)
- [\[aria-expanded\]](https://wonderium.dev/a11y/aria-expanded.md)

---

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