<button id="toggleBtn">Toggle popover</button>
<div id="mypopover">This is popover content!</div>HTMLButtonElement: popoverTargetElement property
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const popover = document.getElementById("mypopover");
const toggleBtn = document.getElementById("toggleBtn");
// Check for popover API support.
function supportsPopover() {
return Object.hasOwn(HTMLElement.prototype, "popover");
}if (supportsPopover()) {
// Set the <div> element to be an auto popover
popover.popover = "auto";
// Set the button popover target to be the popover
toggleBtn.popoverTargetElement = popover;
// Set that the button toggles popover visibility
toggleBtn.popoverTargetAction = "toggle";
} else {
popover.textContent = "Popover API not supported.";
toggleBtn.hidden = true;
}