HTMLElement: togglePopover() method

Baseline 2024
Newly available

Since April 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

The togglePopover() method of the HTMLElement interface toggles a popover element (i.e., one that has a valid popover attribute) between the hidden and showing states.

When togglePopover() is called on an element with the popover attribute:

  1. A beforetoggle event is fired.
  2. The popover toggles between hidden and showing:
    1. If it was initially showing, it toggles to hidden.
    2. If it was initially hidden, it toggles to showing.
  3. A toggle event is fired.

Syntax

js
togglePopover()
togglePopover(force)
togglePopover(options)

Parameters

A boolean (force) or an options object:

force Optional

A boolean, which causes togglePopover() to behave like showPopover() or hidePopover(), except that it doesn't throw an exception if the popover is already in the target state.

  • If set to true, the popover is shown if it was initially hidden. If it was initially shown, nothing happens.
  • If set to false, the popover is hidden if it was initially shown. If it was initially hidden, nothing happens.
options Optional

An object that can contain the following properties:

force Optional

A boolean; see the force description above.

source Optional

An HTMLElement reference; programmatically defines the invoker of the popover associated with the toggle action, that is, its control element. Establishing a relationship between a popover and its invoker using the source option has two useful effects:

  • The browser places the popover in a logical position in the keyboard focus navigation order when shown. This makes the popover more accessible to keyboard users (see also Popover accessibility features).
  • The browser creates an implicit anchor reference between the two, making it very convenient to position popovers relative to their controls using CSS anchor positioning. See Popover anchor positioning for more details.

Return value

true if the popup is open after the call, and false otherwise.

None (undefined) may be returned in older browser versions (see browser compatibility).

Examples

See the Popover API examples landing page to access the full collection of MDN popover examples.

Simple auto-popup

This is a slightly modified version of the Toggle Help UI Popover Example. The example toggles a popover on and off by pressing a particular key on the keyboard (when the example window has focus).

The HTML for the example is shown below. This first element defines instructions on how to invoke the popup, which we need because popups are hidden by default.

html
<p id="instructions">
  Press "h" to toggle a help screen (select example window first).
</p>

We then define a <div> element which is the popup. The actual content doesn't matter, but note that we need the popover attribute to make the <div> into a popover so that it is hidden by default (or we could set this element in the JavaScript).

html
<div id="mypopover" popover>
  <h2>Help!</h2>

  <p>You can use the following commands to control the app</p>

  <ul>
    <li>Press <ins>C</ins> to order cheese</li>
    <li>Press <ins>T</ins> to order tofu</li>
    <li>Press <ins>B</ins> to order bacon</li>
  </ul>
</div>

The JavaScript for the example is shown below. First we check whether popovers are supported, and if they aren't we hide the popover div so that it isn't displayed inline.

js
const instructions = document.getElementById("instructions");
const popover = document.getElementById("mypopover");

if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
  popover.innerText = "";
  instructions.innerText = "Popovers not supported";
}

If popovers are supported we add a listener for the h key to be pressed, and use that to trigger opening the popup. We also log whether the popup was open or closed after the call, but only if a true or false was returned.

js
if (Object.hasOwn(HTMLElement.prototype, "popover")) {
  document.addEventListener("keydown", (event) => {
    if (event.key === "h") {
      const popupOpened = popover.togglePopover();

      // Check if popover is opened or closed on supporting browsers
      if (popupOpened !== undefined) {
        instructions.innerText +=
          popupOpened === true ? `\nOpened` : `\nClosed`;
      }
    }
  });
}

You can test this out using the live example below.

Specifications

Specification
HTML
# dom-togglepopover

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
togglePopover
Chrome – Full support
Chrome 114 (Release date: 2023-05-30)
footnote Full support
Edge – Full support
Edge 114 (Release date: 2023-06-02)
footnote Full support
Firefox – Full support
Firefox 125 (Release date: 2024-04-16)
footnote Full support
Opera – Full support
Opera 100 (Release date: 2023-06-29)
footnote Full support
Safari – Full support
Safari 17 (Release date: 2023-09-18)
footnote Full support
Chrome Android – Full support
Chrome Android 114 (Release date: 2023-05-30)
footnote Full support
Firefox for Android – Full support
Firefox for Android 125 (Release date: 2024-04-16)
footnote Full support
Opera Android – Full support
Opera Android 76 (Release date: 2023-06-26)
footnote Full support
Safari on iOS – Full support
Safari on iOS 17 (Release date: 2023-09-18)
footnote Full support
Samsung Internet – Full support
Samsung Internet 23 (Release date: 2023-10-18)
footnote Full support
WebView Android – Full support
WebView Android 114 (Release date: 2023-05-30)
footnote Full support
WebView on iOS – Full support
WebView on iOS 17 (Release date: 2023-09-18)
footnote Full support
force parameter
Chrome – Full support
Chrome 116 (Release date: 2023-08-15)
footnote Full support
Edge – Full support
Edge 116 (Release date: 2023-08-21)
footnote Full support
Firefox – Full support
Firefox 141 (Release date: 2025-07-22)
footnote Full support
Opera – Full support
Opera 102 (Release date: 2023-08-23)
footnote Full support
Safari – Full support
Safari 17 (Release date: 2023-09-18)
footnote Full support
Chrome Android – Full support
Chrome Android 116 (Release date: 2023-08-15)
footnote Full support
Firefox for Android – Full support
Firefox for Android 141 (Release date: 2025-07-22)
footnote Full support
Opera Android – Full support
Opera Android 78 (Release date: 2023-10-23)
footnote Full support
Safari on iOS – Full support
Safari on iOS 17 (Release date: 2023-09-18)
footnote Full support
Samsung Internet – Full support
Samsung Internet 24 (Release date: 2024-01-25)
footnote Full support
WebView Android – Full support
WebView Android 116 (Release date: 2023-08-15)
footnote Full support
WebView on iOS – Full support
WebView on iOS 17 (Release date: 2023-09-18)
footnote Full support
options.force parameter
Chrome – Full support
Chrome 130 (Release date: 2024-10-15)
footnote Full support
Edge – Full support
Edge 130 (Release date: 2024-10-17)
footnote Full support
Firefox – Full support
Firefox 141 (Release date: 2025-07-22)
footnote Full support
Opera – Full support
Opera 115 (Release date: 2024-11-27)
footnote Full support
Safari – Full support
Safari 18.4 (Release date: 2025-03-31)
footnote Full support
Chrome Android – Full support
Chrome Android 130 (Release date: 2024-10-15)
footnote Full support
Firefox for Android – Full support
Firefox for Android 141 (Release date: 2025-07-22)
footnote Full support
Opera Android – Full support
Opera Android 86 (Release date: 2024-12-02)
footnote Full support
Safari on iOS – Full support
Safari on iOS 18.4 (Release date: 2025-03-31)
footnote Full support
Samsung Internet – Full support
Samsung Internet 28 (Release date: 2025-04-02)
footnote Full support
WebView Android – Full support
WebView Android 130 (Release date: 2024-10-15)
footnote Full support
WebView on iOS – Full support
WebView on iOS 18.4 (Release date: 2025-03-31)
footnote Full support
options.source parameter
Chrome – Partial support
Chrome 133 – 136 (Release date: 2025-02-04)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 383343310.
Chrome – Full support
Chrome 137 (Release date: 2025-05-27)
footnote Full support
Edge – Partial support
Edge 133 – 136 (Release date: 2025-02-06)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 383343310.
Edge – Full support
Edge 137 (Release date: 2025-05-29)
footnote Full support
Firefox – Partial support
Firefox 141 – 143 (Release date: 2025-07-22)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 1984004.
Firefox – Full support
Firefox 144 (Release date: 2025-10-14)
footnote Full support
Opera – Partial support
Opera 118 – 120 (Release date: 2025-04-15)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 383343310.
Opera – Full support
Opera 121 (Release date: 2025-08-27)
footnote Full support
Safari – Partial support
Safari 18.4 – 18.6 (Release date: 2025-03-31)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 286575.
Safari – Full support
Safari 26 (Release date: 2025-09-15)
footnote Full support
Chrome Android – Partial support
Chrome Android 133 – 136 (Release date: 2025-02-04)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 383343310.
Chrome Android – Full support
Chrome Android 137 (Release date: 2025-05-27)
footnote Full support
Firefox for Android – Partial support
Firefox for Android 141 – 143 (Release date: 2025-07-22)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 1984004.
Firefox for Android – Full support
Firefox for Android 144 (Release date: 2025-10-14)
footnote Full support
Opera Android – Partial support
Opera Android 88 – 89 (Release date: 2025-03-19)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 383343310.
Opera Android – Full support
Opera Android 90 (Release date: 2025-06-18)
footnote Full support
Safari on iOS – Partial support
Safari on iOS 18.4 – 18.6 (Release date: 2025-03-31)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 286575.
Safari on iOS – Full support
Safari on iOS 26 (Release date: 2025-09-15)
footnote Full support
Samsung Internet – Partial support
Samsung Internet 29 (Release date: 2025-10-25)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 383343310.
WebView Android – Partial support
WebView Android 133 – 136 (Release date: 2025-02-04)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 383343310.
WebView Android – Full support
WebView Android 137 (Release date: 2025-05-27)
footnote Full support
WebView on iOS – Partial support
WebView on iOS 18.4 – 18.6 (Release date: 2025-03-31)
footnote Partial support
footnote When using this option, the focus order doesn't change, so the popover does not become the next focus element. See bug 286575.
WebView on iOS – Full support
WebView on iOS 26 (Release date: 2025-09-15)
footnote Full support
Implicit anchor reference via source
Chrome – Full support
Chrome 133 (Release date: 2025-02-04)
footnote Full support
Edge – Full support
Edge 133 (Release date: 2025-02-06)
footnote Full support
Firefox – Full support
Firefox 147 (Release date: 2026-01-13)
footnote Full support
Opera – Full support
Opera 118 (Release date: 2025-04-15)
footnote Full support
Safari – Full support
Safari 26 (Release date: 2025-09-15)
footnote Full support
Chrome Android – Full support
Chrome Android 133 (Release date: 2025-02-04)
footnote Full support
Firefox for Android – Full support
Firefox for Android 147 (Release date: 2026-01-13)
footnote Full support
Opera Android – Full support
Opera Android 88 (Release date: 2025-03-19)
footnote Full support
Safari on iOS – Full support
Safari on iOS 26 (Release date: 2025-09-15)
footnote Full support
Samsung Internet – Full support
Samsung Internet 29 (Release date: 2025-10-25)
footnote Full support
WebView Android – Full support
WebView Android 133 (Release date: 2025-02-04)
footnote Full support
WebView on iOS – Full support
WebView on iOS 26 (Release date: 2025-09-15)
footnote Full support
Returns true or false
Chrome – Full support
Chrome 116 (Release date: 2023-08-15)
footnote Full support
Edge – Full support
Edge 116 (Release date: 2023-08-21)
footnote Full support
Firefox – Full support
Firefox 125 (Release date: 2024-04-16)
footnote Full support
Opera – Full support
Opera 102 (Release date: 2023-08-23)
footnote Full support
Safari – Full support
Safari 17 (Release date: 2023-09-18)
footnote Full support
Chrome Android – Full support
Chrome Android 116 (Release date: 2023-08-15)
footnote Full support
Firefox for Android – Full support
Firefox for Android 125 (Release date: 2024-04-16)
footnote Full support
Opera Android – Full support
Opera Android 78 (Release date: 2023-10-23)
footnote Full support
Safari on iOS – Full support
Safari on iOS 17 (Release date: 2023-09-18)
footnote Full support
Samsung Internet – Full support
Samsung Internet 24 (Release date: 2024-01-25)
footnote Full support
WebView Android – Full support
WebView Android 116 (Release date: 2023-08-15)
footnote Full support
WebView on iOS – Full support
WebView on iOS 17 (Release date: 2023-09-18)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Has more compatibility info.

See also