ToggleEvent: source property

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The source read-only property of the ToggleEvent interface is an Element object instance representing the HTML popover control element that initiated the toggle.

Value

An Element object instance, or null if the popover was not activated by a control element.

Description

A <button> element can be set as a popover control by specifying the id of the popover element in its commandfor or popovertarget attribute (if the button is specified using <input type="button">, only the popovertarget attribute works).

When the toggle event fires on the popover, the ToggleEvent event object's source property will then contain a reference to the popover control button that initiated the toggle. This is useful for running different code in response to the toggle event depending on which popover control initiated it (see an example).

Before the source property existed, you had to manually reimplement the command attribute functionality in JavaScript to provide a similar identifier and then monitor it to identify which button invoked the popover. In addition, there was a danger of such JavaScript tasks blocking the showing or hiding of the popover. The toggle event is asynchronous, and therefore avoids this problem.

If the popover element is not activated by a control button — for example, if it's controlled using a JavaScript method such as HTMLElement.togglePopover() — the source property is null.

Examples

Basic source usage

This demo shows how to use the source property to perform different actions based on the control button that closes the popover element.

HTML

Our markup contains a <button>, a <p>, and a <div> element. The <div> is designated as an auto popover, and the button is designated as a control for showing the popover using the commandfor and command attributes. The popover contains a heading asking the user if they would like a cookie, and two buttons labeled Yes and No allowing them to select an answer. Each of these buttons is designated as a control for hiding the popover.

html
<button commandfor="popover" command="show-popover">
  Select cookie preference
</button>
<p id="output"></p>
<div id="popover" popover="auto">
  <h3>Would you like a cookie?</h3>
  <button id="yes" commandfor="popover" command="hide-popover">Yes</button>
  <button id="no" commandfor="popover" command="hide-popover">No</button>
</div>

JavaScript

In our script, we start off by grabbing references to the "yes" and "no" buttons, the popover, and the output <p>.

js
const yesBtn = document.getElementById("yes");
const noBtn = document.getElementById("no");
const popover = document.getElementById("popover");
const output = document.getElementById("output");

We've added feature detection for the HTML command attribute and the source property. If either is not supported in the browser, we print a message in the <p> element. If both are supported, we add a toggle event listener to the popover element. When the event fires, the code checks if the "yes" or "no" button was used to toggle (hide) the popover element; an appropriate message is printed to the output <p> in each case.

js
if (yesBtn.command === undefined) {
  output.textContent = "Popover control command attribute not supported.";
} else {
  popover.addEventListener("toggle", (event) => {
    if (event.source === undefined) {
      output.textContent = "ToggleEvent.source not supported.";
    } else if (event.source === yesBtn) {
      output.textContent = "Cookie set!";
    } else if (event.source === noBtn) {
      output.textContent = "No cookie set.";
    }
  });
}

Result

Specifications

Specification
HTML
# dom-toggleevent-source

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
source
Chrome – Full support
Chrome 140 (Release date: 2025-09-02)
footnote Full support
Edge – Full support
Edge 140 (Release date: 2025-09-05)
footnote Full support
Firefox – Full support
Firefox 145 (Release date: 2025-11-11)
footnote Full support
Opera – Full support
Opera 124 (Release date: 2025-11-13)
footnote Full support
Safari – Preview support
Safari 26.5
footnote Preview browser support
Chrome Android – Full support
Chrome Android 140 (Release date: 2025-09-02)
footnote Full support
Firefox for Android – Full support
Firefox for Android 145 (Release date: 2025-11-11)
footnote Full support
Opera Android – Full support
Opera Android 92 (Release date: 2025-10-08)
footnote Full support
Safari on iOS – Preview support
Safari on iOS 26.5
footnote Preview browser support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Full support
WebView Android 140 (Release date: 2025-09-02)
footnote Full support
WebView on iOS – Preview support
WebView on iOS 26.5
footnote Preview browser support

Legend

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

Full support
Full support
In development. Supported in a pre-release version.
In development. Supported in a pre-release version.
No support
No support

See also