Skip to content

Commit 887acda

Browse files
Fixes to beforetoggle event page (mdn#43494)
* Fixes to beforetoggle event page * Apply suggestions from code review Co-authored-by: Hamish Willee <hamishwillee@gmail.com> --------- Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
1 parent 63fee14 commit 887acda

File tree

1 file changed

+8
-25
lines changed
  • files/en-us/web/api/htmlelement/beforetoggle_event

1 file changed

+8
-25
lines changed

files/en-us/web/api/htmlelement/beforetoggle_event/index.md

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ A {{domxref("ToggleEvent")}}. Inherits from {{domxref("Event")}}.
3838

3939
## Examples
4040

41-
The examples below demonstrates how the `beforetoggle` event might be used for a {{domxref("Popover_API", "popover", "", "nocode")}} or {{htmlelement("dialog")}} element.
42-
The same examples would work similarly on the other element types.
41+
The examples below demonstrate how the `beforetoggle` event might be used for a {{domxref("Popover_API", "popover", "", "nocode")}} element.
42+
The same examples would work similarly on a {{htmlelement("dialog")}} element.
4343

4444
### Basic example
4545

@@ -77,7 +77,7 @@ function log(text) {
7777

7878
#### JavaScript
7979

80-
The code gets adds an event listener for the `beforetoggle` event and logs the state.
80+
The code adds an event listener for the `beforetoggle` event and logs the state.
8181

8282
```js
8383
const popover = document.getElementById("mypopover");
@@ -99,12 +99,12 @@ popover.addEventListener("beforetoggle", (event) => {
9999

100100
The `beforetoggle` event is cancelable if fired when opening an element.
101101

102-
Below we show how a popover that might first check if it is allowed to open, and if not, call {{domxref("Event.preventDefault()")}} to cancel the event.
103-
In this example we use a button to set whether the popover can open or not: in a more "full featured" example this might depend on the application state, or the data in the popover being ready to display.
102+
Below we show how a popover might first check if it is allowed to open, and if not, call {{domxref("Event.preventDefault()")}} to cancel the event.
103+
In this example we use a checkbox to set whether the popover can open or not: in a more "full featured" example this might depend on the application state, or the data in the popover being ready to display.
104104

105105
#### HTML
106106

107-
The HTML consists of a popover, a button for toggling it open and closed, and a button for setting whether the button can be opened.
107+
The HTML consists of a popover, a button for toggling it open and closed, and a checkbox for setting whether the popover can be opened.
108108

109109
```html
110110
<button popovertarget="mypopover">Toggle the popover</button>
@@ -137,7 +137,7 @@ function log(text) {
137137

138138
#### JavaScript
139139

140-
First we set up the code to simulate a state where we don't want to allow the popover to open.
140+
First we set up the code to simulate a state where we want to allow the popover to open.
141141
This is represented by the variable `allowOpen`, which is toggled when the associated checkbox is toggled.
142142

143143
```js
@@ -150,7 +150,7 @@ allowCheckbox.addEventListener("change", (event) => {
150150
});
151151
```
152152

153-
The code gets adds an event listener for the `beforetoggle` event.
153+
The code adds an event listener for the `beforetoggle` event.
154154
If `allowOpen` is false then `preventDefault()` is called, which stops the popup from opening.
155155

156156
```js
@@ -174,23 +174,6 @@ popover.addEventListener("beforetoggle", (event) => {
174174

175175
{{EmbedLiveSample("Prevent a popover opening", '100%', "250px")}}
176176

177-
### A note on beforetoggle event coalescing
178-
179-
If multiple `beforetoggle` events are fired before the event loop has a chance to cycle, only a single event will be fired.
180-
This is referred to as "event coalescing".
181-
182-
For example:
183-
184-
```js
185-
popover.addEventListener("beforetoggle", () => {
186-
//
187-
});
188-
189-
popover.showPopover();
190-
popover.hidePopover();
191-
// `beforetoggle` only fires once
192-
```
193-
194177
### Other examples
195178

196179
- [Opening a modal dialog](/en-US/docs/Web/API/HTMLDialogElement#open_close_a_modal_dialog) example in `HTMLDialogElement`

0 commit comments

Comments
 (0)