|
| 1 | +--- |
| 2 | +title: "ToggleEvent: source property" |
| 3 | +short-title: source |
| 4 | +slug: Web/API/ToggleEvent/source |
| 5 | +page-type: web-api-instance-property |
| 6 | +browser-compat: api.ToggleEvent.source |
| 7 | +--- |
| 8 | + |
| 9 | +{{APIRef("Popover API")}} |
| 10 | + |
| 11 | +The **`source`** read-only property of the {{domxref("ToggleEvent")}} interface is an {{domxref("Element")}} object instance representing the HTML popover control element that initiated the toggle. |
| 12 | + |
| 13 | +## Value |
| 14 | + |
| 15 | +An {{domxref("Element")}} object instance, or `null` if the popover was not activated by a control element. |
| 16 | + |
| 17 | +## Description |
| 18 | + |
| 19 | +A {{htmlelement("button")}} element can be set as a popover control by specifying the [`id`](/en-US/docs/Web/HTML/Reference/Global_attributes/id) of the popover element in its [`commandfor`](/en-US/docs/Web/HTML/Reference/Elements/button#commandfor) or [`popovertarget`](/en-US/docs/Web/HTML/Reference/Elements/button#popovertarget) attribute (if the button is specified using `<input type="button">`, only the `popovertarget` attribute works). |
| 20 | + |
| 21 | +When the [`toggle`](/en-US/docs/Web/API/HTMLElement/toggle_event) 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 control initiated it (see an [example](#basic_source_usage)). |
| 22 | + |
| 23 | +Before the `source` property was available, developers would have to reimplement the `command` attribute functionality from scratch to provide a similar identifier and then monitor it with JavaScript to know which button invoked the popover. |
| 24 | +In addition, there was a danger of such JavaScript tasks blocking the showing or hiding of the popover. |
| 25 | +The `toggle` event is asynchronous, and therefore avoids this problem. |
| 26 | + |
| 27 | +If the popover was not activated by a control button — for example, if the popover is being controlled using a JavaScript method such as {{domxref("HTMLElement.togglePopover()")}} — the `source` property returns `null`. |
| 28 | + |
| 29 | +## Examples |
| 30 | + |
| 31 | +### Basic `source` usage |
| 32 | + |
| 33 | +This demo shows how to use the `source` property to perform a different action depending on which control button was used to close a popover. |
| 34 | + |
| 35 | +#### HTML |
| 36 | + |
| 37 | +Our markup contains a `<button>`, a {{htmlelement("p")}}, and a {{htmlelement("div")}} element. The `<div>` is designated as an [`auto` popover](/en-US/docs/Web/API/Popover_API/Using#auto_state_and_light_dismiss), and the button is designated as a control for showing the popover using the [`commandfor`](/en-US/docs/Web/HTML/Reference/Elements/button#commandfor) and [`command`](/en-US/docs/Web/HTML/Reference/Elements/button#command) attributes. |
| 38 | + |
| 39 | +The popover contains a heading asking the user if they would like a cookie, and two buttons allowing them to select an answer of "yes" or "no". Each one of these buttons is designated as a control for hiding the popover. |
| 40 | + |
| 41 | +```html live-sample___toggleevent-source |
| 42 | +<button commandfor="popover" command="show-popover"> |
| 43 | + Select cookie preference |
| 44 | +</button> |
| 45 | +<p id="output"></p> |
| 46 | +<div id="popover" popover="auto"> |
| 47 | + <h3>Would you like a cookie?</h3> |
| 48 | + <button id="yes" commandfor="popover" command="hide-popover">Yes</button> |
| 49 | + <button id="no" commandfor="popover" command="hide-popover">No</button> |
| 50 | +</div> |
| 51 | +``` |
| 52 | + |
| 53 | +```css hidden live-sample___toggleevent-source |
| 54 | +html { |
| 55 | + font-family: sans-serif; |
| 56 | +} |
| 57 | + |
| 58 | +[popover] { |
| 59 | + border: 1px solid grey; |
| 60 | + padding: 10px 20px; |
| 61 | + border-radius: 5px; |
| 62 | +} |
| 63 | + |
| 64 | +[popover] h3 { |
| 65 | + margin: 0 0 10px; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +#### JavaScript |
| 70 | + |
| 71 | +In our script, we start off by grabbing references to the "yes" and "no" buttons, the popover, and the output `<p>`. |
| 72 | + |
| 73 | +```js live-sample___toggleevent-source |
| 74 | +const yesBtn = document.getElementById("yes"); |
| 75 | +const noBtn = document.getElementById("no"); |
| 76 | +const popover = document.getElementById("popover"); |
| 77 | +const output = document.getElementById("output"); |
| 78 | +``` |
| 79 | + |
| 80 | +We then add some feature detection to detect whether the HTML `command` attribute is supported, and whether the `source` property is supported. If either are not supported, we output an appropriate message to the output `<p>`. If both are supported, we add a [`toggle`](/en-US/docs/Web/API/HTMLElement/toggle_event) event listener to the popover. When fired, it checks if the "yes" or the "no" button was used to toggle (hide) the popover; an appropriate message is printed to the output `<p>` in each case. |
| 81 | + |
| 82 | +```js live-sample___toggleevent-source |
| 83 | +if (yesBtn.command === undefined) { |
| 84 | + output.textContent = "Popover control command attribute not supported."; |
| 85 | +} else { |
| 86 | + popover.addEventListener("toggle", (event) => { |
| 87 | + if (event.source === undefined) { |
| 88 | + output.textContent = "ToggleEvent.source not supported."; |
| 89 | + } else if (event.source === yesBtn) { |
| 90 | + output.textContent = "Cookie set!"; |
| 91 | + } else if (event.source === noBtn) { |
| 92 | + output.textContent = "No cookie set."; |
| 93 | + } |
| 94 | + }); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +#### Result |
| 99 | + |
| 100 | +{{embedlivesample("toggleevent-source", "100%", "100")}} |
| 101 | + |
| 102 | +## Specifications |
| 103 | + |
| 104 | +{{Specifications}} |
| 105 | + |
| 106 | +## Browser compatibility |
| 107 | + |
| 108 | +{{Compat}} |
| 109 | + |
| 110 | +## See also |
| 111 | + |
| 112 | +- [Popover API](/en-US/docs/Web/API/Popover_API) |
0 commit comments