Sub topic 7
Controlling popovers from JavaScript
This chapter assumes you can write basic HTML, select elements with document.getElementById, and attach event listeners. You should also know how popovertarget buttons open a popover without any script, and the difference between auto and manual popovers.
Invoker buttons cover most cases. You need JavaScript when something other than a button click decides whether the popover shows, such as a timer, a network response, or a keyboard shortcut.
The three methods
Every element with a popover attribute has three methods.
| Method | What it does |
|---|---|
| `showPopover()` | Opens the popover. |
| `hidePopover()` | Closes the popover. |
| `togglePopover()` | Opens it if closed, closes it if open. Returns `true` if the popover is now open. |
The manual example earlier used showPopover() to display a notice on load. Here is a fuller version that shows a notice after a simulated save and hides it after three seconds.
<button id="save">Save</button>
<div id="notice" popover="manual">
<p>Your changes were saved.</p>
</div>
<script>
const notice = document.getElementById('notice');
document.getElementById('save').addEventListener('click', () => {
notice.showPopover();
setTimeout(() => notice.hidePopover(), 3000);
});
</script>The notice uses popover="manual" because an auto popover would close when the user clicked elsewhere, and a timed message should not depend on that.
Checking state before you act
Calling showPopover() on a popover that is already open does nothing. Calling hidePopover() on one that is closed also does nothing. Older browsers and some specification drafts threw an error in these cases, so write code that does not depend on either behavior.
The reliable way to read the current state is the :popover-open selector, which you can test with matches().
const panel = document.getElementById('panel');
function isOpen(el) {
return el.matches(':popover-open');
}
document.addEventListener('keydown', (event) => {
if (event.key === '?' && !isOpen(panel)) {
panel.showPopover();
}
});Do not track state in your own boolean variable. Light dismiss, the Escape key, and other popovers opening can all close a popover without running your code, so your variable would drift out of sync.
Common errors
Three mistakes account for most failures.
- Calling a method on an element without the attribute.
showPopover()throws aNotSupportedErrorif the element has nopopoverattribute. Add the attribute in the HTML. - Opening a popover that is not connected to the document. If the element is not in the page yet, the call throws an
InvalidStateError. Append it first, then show it. - Expecting the popover to stay open. With
popover="auto", a click outside closes it. A popover you open from aclickhandler can also be closed by that same click if the handler runs on an element outside it. Usemanualor open it from the invoking button's handler.
Choosing between script and invoker buttons
Use popovertarget when a user click on a button is the only trigger. It needs no code and works before your script loads. Use the methods when the trigger is not a button, or when you must do work before the popover opens, such as loading content.
flowchart TDA decision rule is simpler than a diagram here, so use this list:
- Button click only: use
popovertarget. - Timer, event, or shortcut: use
showPopover(). - Need to read state: use
matches(':popover-open'). - Need to run code when state changes: listen for
toggle, covered in the next chapter.
Exercise
A popover with popover="auto" is open. Your code calls panel.showPopover() again. What happens, and how do you check whether the popover is open without keeping your own variable?
Show answer
Nothing happens, because the popover is already open. Check with panel.matches(':popover-open'), since light dismiss and Escape can close the popover without running your code.