<h1>Example: focus on dialog close; focusable children of dialog</h1>
<p tabindex="0">I am focusable</p>
<dialog id="favDialog">
<form method="dialog">
<p><label>Favorite animal:
<select>
<option></option>
<option>Brine shrimp</option>
<option>Red panda</option>
<option>Spider monkey</option>
</select>
</label></p>
<menu>
<button id="cancel" value="cancel">Cancel</button>
<button id="confirmBtn" value="default">Confirm</button>
</menu>
</form>
</dialog>
<button id="updateDetails">Update details</button>
<p tabindex="0">I am focusable</p>
<output aria-live="polite"></output>
<h2>Findings</h2>
<ul>
<li>Focus remains on whichever child element of dialog had focus at time of closure, including if dismissed by ESC key</li>
<li>Pressing tab will move focus to the next focusable element in the root document (as opposed to next focusable element in hidden dialog)</li>
</ul>
var updateButton = document.getElementById('updateDetails');
var favDialog = document.getElementById('favDialog');
var outputBox = document.querySelector('output');
var selectEl = document.querySelector('select');
var confirmBtn = document.getElementById('confirmBtn');
// "Update details" button opens the <dialog> modally
updateButton.addEventListener('click', function onOpen() {
if (typeof favDialog.showModal === "function") {
favDialog.showModal();
outputBox.value = "";
} else {
alert("The <dialog> API is not supported by this browser");
}
});
// "Favorite animal" input sets the value of the submit button
selectEl.addEventListener('change', function onSelect(e) {
confirmBtn.value = selectEl.value;
});
/*// "Confirm" button of form triggers "close" on dialog because of [method="dialog"]
favDialog.addEventListener('close', function onClose() {
updateOutput();
});
// Add event listener to ESC key
document.onkeydown = function(e) {
if (e.keyCode === 27) {
updateOutput();
}
};*/
setInterval(() => { outputBox.value = "Active element is " + document.activeElement}, 200);
function updateOutput() {
outputBox.value = "Active element is " + document.activeElement;
if (document.activeElement.id) {
outputBox.value = outputBox.value + " with #" + document.activeElement.id;
}
}
favDialog.addEventListener("close", () => console.log("close event"));
favDialog.addEventListener("cancel", () => console.log("cancel event"));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.