Sub topic 2
Opening and closing with popovertarget and popovertargetaction
This chapter covers the two attributes that connect a button to a popover. They let you open, close, and toggle a popover with no JavaScript. It assumes you know basic HTML elements and attributes, can read simple CSS selectors, and know how buttons behave with a mouse and a keyboard. The previous chapter showed that the popover attribute hides an element until something shows it. This chapter covers the "something".
The invoker relationship
An invoker is a control that opens or closes a popover. You make a button an invoker by adding popovertarget, whose value is the id of the popover.
<button popovertarget="help">Show help</button>
<div id="help" popover>
<p>Press Escape or click outside this box to close it.</p>
</div>When the user activates the button with a click, Enter, or Space, the browser looks up the element with that id. If the element is a popover, the browser changes its state. Because this uses the button's normal activation behavior, keyboard users get it for free. You don't need a click listener or a tabindex.
Some details of the lookup:
- The id is resolved in the same tree as the button. An invoker in the main document cannot target a popover inside a shadow root, and the reverse is also true.
- If no element has that id, or the element has no
popoverattribute, the click does nothing and the browser reports no error. - The invoker does not need to sit next to the popover in the markup. It can be anywhere in the same tree.
- Several buttons can target the same popover. Each one acts independently.
Which elements can be invokers
popovertarget works on <button> elements. It also works on <input> elements whose type is button, image, reset, or submit. It has no effect on links, <div> elements, or other inputs. A disabled button does not invoke anything.
If the button sits inside a <form>, a button with no type is a submit button by default. In that case the click can also submit the form. Set type="button" on invokers in forms unless you want both effects.
<form action="/subscribe" method="post">
<input name="email" type="email" required>
<button type="button" popovertarget="privacy">What do you do with my email?</button>
<button type="submit">Subscribe</button>
</form>
<div id="privacy" popover>
<p>We use your address only to send the newsletter.</p>
</div>Choosing the action with popovertargetaction
By default an invoker toggles: it opens a closed popover and closes an open one. The popovertargetaction attribute changes that behavior.
| Value | Effect on a closed popover | Effect on an open popover |
|---|---|---|
| `toggle` | Opens it | Closes it |
| `show` | Opens it | Nothing |
| `hide` | Nothing | Closes it |
Omitting the attribute is the same as toggle. An unrecognized value, such as a typo like popovertargetaction="close", also falls back to toggle. Because the fallback is silent, a mistyped hide button will still seem to work in some states and fail in others. Check the spelling first when a close button misbehaves.
The next example shows all three values together. The "Open" and "Close" buttons are one-way controls. The "Menu" button toggles.
<button popovertarget="menu" popovertargetaction="toggle">Menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<a href="/settings">Settings</a>
<button popovertarget="menu" popovertargetaction="hide">Close</button>
</div>The close button lives inside the popover and targets it by id. This is the usual pattern for a dismiss control. It matters most for manual popovers, which ignore outside clicks and Escape (covered in a later chapter).
What happens on a click
The browser follows the same steps for every invoker activation.
Two consequences follow from this flow. First, show and hide are idempotent: repeating them is safe, and they never flip the state back. Second, toggle depends on the current state at the moment of the click, not on the state when the page loaded.
Clicking the invoker of an open popover
A popover in the default auto mode closes when you click outside it. The invoker is outside the popover, so you might expect this sequence for a toggle button: the outside click closes the popover, and then the toggle reopens it. Browsers avoid this. A click on the popover's own invoker counts as part of the popover's interaction, so the toggle closes it once and it stays closed. You do not need to write code for this case.
Using a popover from several invokers
A single popover can have more than one invoker, and each can use a different action. A common case is a toolbar with explicit "Show" and "Hide" buttons, or a dialog-like panel that opens from a button and closes from a footer.
<button type="button" popovertarget="log" popovertargetaction="show">Show log</button>
<button type="button" popovertarget="log" popovertargetaction="hide">Hide log</button>
<section id="log" popover="manual">
<h2>Activity log</h2>
<p>No recent activity.</p>
<button type="button" popovertarget="log" popovertargetaction="hide">Close</button>
</section>Here popover="manual" keeps the log open until one of the hide buttons is used, so the explicit buttons are the only way to close it with the mouse. Plain popover would also close it on Escape or on an outside click.
Reading and setting the attributes from JavaScript
Both attributes have DOM properties. This is useful when you build the interface dynamically.
const button = document.createElement('button');
button.type = 'button';
button.textContent = 'Details';
const details = document.getElementById('details');
// Set the target with an element reference instead of an id string
button.popoverTargetElement = details;
button.popoverTargetAction = 'toggle';
document.body.append(button);popoverTargetElement accepts an element reference, so you do not have to give the popover an id. This helps when the popover is created in code. Assigning the property sets the invoker relationship, and popoverTargetAction accepts the same three strings as the attribute. If the target and the button are in different trees, such as separate shadow roots, the reference form is not a reliable workaround, so test the case you actually need.
To open a popover from code that is not tied to a button, use the methods from the JavaScript chapter instead.
Accessibility of invokers
Because the invoker is a real <button>, it already has a role, a focus stop, and keyboard activation. Browsers also expose the connection between the button and its popover to assistive technology, including whether the popover is open. Don't add aria-expanded or aria-controls by hand to a popovertarget button. Duplicated state can fall out of sync and lead to conflicting announcements.
Give every invoker a text label that says what it does. An icon-only button needs an accessible name, for example through aria-label. A hide button inside the popover should say "Close" or "Dismiss", not "X" alone.
Common errors
- The id does not match. A wrong or missing id fails silently. Check that the value of
popovertargetequals the popover'sidexactly, including case. - The target has no
popoverattribute. The id matches an element, but nothing opens. Addpopoverto the target. - A submit button submits the form. Without
type="button", an invoker in a form can also trigger form submission and a page reload. - A misspelled action. An invalid
popovertargetactionvalue becomestoggle. - Duplicate ids. If two elements share an id, the browser uses the first one in tree order, which may not be the one you meant.
- A disabled button. A disabled invoker does nothing, and it cannot be focused.
Summary
popovertargettakes the id of a popover and turns a button into its invoker.popovertargetactionselectsshow,hide, ortoggle. A missing or invalid value meanstoggle.- Invokers work on
<button>and on<input>of typebutton,image,reset, orsubmit. - Several invokers can control one popover, and a hide button can live inside the popover.
- Set
type="button"inside forms, and let the browser handle the accessibility state of the invoker.
Exercise
A page has <button popovertarget="tips" popovertargetaction="close">Tips</button> and <div id="tips" popover>...</div>. A user clicks the button twice. What happens, and what is the fix to make the button only ever open the popover?
Show answer
close is not a valid value, so the browser treats it as toggle. The first click opens the popover and the second click closes it. To make the button only open it, use popovertargetaction="show". Add a separate button with popovertargetaction="hide" if users need a way to close it.
Exercise
An invoker button inside a <form> opens its popover, but the page also reloads. Why does this happen, and how do you stop it?
Show answer
A <button> without a type attribute defaults to type="submit", so activating it also submits the form. Add type="button" to the invoker so it only changes the popover state.