Sub topic 2
Opening and closing with popovertarget and popovertargetaction
This chapter assumes you know basic HTML elements and attributes, and that you have used a <button> in a page. You do not need JavaScript here. The goal is to wire a button to a popover using only attributes.
The invoker button
A popover is an element with the popover attribute. Until something opens it, the browser keeps it hidden. The simplest way to open it is an invoker: a <button> (or <input type="button">) with a popovertarget attribute. The attribute's value is the id of the popover to control.
<button popovertarget="help">Show help</button>
<div id="help" popover>
<p>Press Escape or click outside this box to close it.</p>
</div>Clicking the button shows the help element. The browser also links the two elements for assistive technology, so you do not need to add aria-expanded or aria-controls yourself.
The invoker can sit anywhere in the document. It does not need to be next to the popover or inside it. It only needs a matching id.
Choosing the action
By default, an invoker toggles: it opens a closed popover and closes an open one. The popovertargetaction attribute changes this. It accepts three values.
| Value | What the button does |
|---|---|
| `toggle` | Opens the popover if closed, closes it if open. This is the default. |
| `show` | Opens the popover. Does nothing if it is already open. |
| `hide` | Closes the popover. Does nothing if it is already closed. |
This lets you put a dedicated close button inside the popover:
<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 first button opens and closes the menu. The second always closes it. Both work with no script.
How the click is handled
Step by step:
- The browser reads
popovertargetand looks up the element with thatid. - If that element has no
popoverattribute, the click does nothing. - Otherwise the browser compares the popover's current state with
popovertargetactionand opens, closes, or leaves it alone.
Common errors
- The
iddoes not match. A typo inpopovertargetfails silently. Check the spelling and that theidis unique on the page. - The target is missing
popover. An element with only anidis never controlled by the invoker. - The invoker is not a button. Only
<button>and<input>buttons supportpopovertarget. A<div>or<a>does not. - The button is inside a form. A
<button>in a form defaults totype="submit". When a button has a validpopovertarget, the popover action runs first, but the submit can still happen. Addtype="button"to prevent a form submission. - Expecting a manual popover to close itself. An
autopopover closes on outside clicks and Escape. Amanualpopover does not. Give a manual popover ahidebutton, as the next example shows.
<div id="notice" popover="manual">
<p>Your changes were saved.</p>
<button popovertarget="notice" popovertargetaction="hide">Dismiss</button>
</div>
<script>
const notice = document.getElementById('notice');
notice.showPopover();
</script>Here a script opens the popover, and the button closes it. Later chapters cover showPopover() and the two popover types in detail.
Exercise
You have a popover with id="tips". Write the markup for a button that only opens it, never closes it, and explain why a button inside a form needs type="button".
Show answer
Use <button type="button" popovertarget="tips" popovertargetaction="show">Tips</button>. The show action opens a closed popover and does nothing if it is already open. Without type="button", a button inside a form defaults to type="submit", so clicking it can also submit the form.
Exercise
A button has popovertarget="menu", but clicking it does nothing. Name two causes you would check.
Show answer
First, check that the id of the popover is exactly "menu" and is unique on the page. Second, check that the element with that id has the popover attribute. Also confirm the invoker is a button or input button, not another element.