4.1 The <dialog> element
HTML5 also introduced the <dialog> element, designed for creating dialog boxes and modal windows on web pages. It makes it easy to build pop-up windows for all sorts of purposes in a semantically correct way: notifications, confirmations, input forms, and much more. Below we'll look at how to use <dialog>, its attributes and methods, plus some usage examples.
The <dialog> element represents a dialog box or some other interactive window. It can be either modal or non-modal. You can put any HTML content inside a <dialog> element: text, forms, buttons, and so on.
Syntax:
<dialog>
<!-- Dialog box content -->
</dialog>
<div style="min-height: 200px;">
<dialog id="favSuperHeroDialog">
<form method="dialog">
<div>
<label for="favSuperHero">Favorite superhero:</label>
<select id="favSuperHero" name="favSuperHero">
<option></option>
<option>Captain America</option>
<option>Iron Man</option>
<option>Hulk</option>
</select>
</div>
<div>
<button type="reset" id="cancel">Reset</button>
<button type="submit">Confirm</button>
</div>
</form>
</dialog>
<button id="updateDetails">Open dialog</button>
</div>
<dialog id="favSuperHeroDialog">
<form method="dialog">
<div>
<label for="favSuperHero">Favorite superhero:</label>
<select id="favSuperHero" name="favSuperHero">
<option></option>
<option>Captain America</option>
<option>Iron Man</option>
<option>Hulk</option>
</select>
</div>
<div>
<button type="reset" id="cancel">Reset</button>
<button type="submit">Confirm</button>
</div>
</form>
</dialog>
<button id="updateDetails">Open dialog</button>
// Get the buttons and the dialog from the DOM
const updateButton = document.getElementById("updateDetails");
const cancelButton = document.getElementById("cancel");
const dialog = document.getElementById("favSuperHeroDialog");
// Function that checks the dialog state (open/closed)
function openCheck(dialog) {
if (dialog.open) {
console.log("Dialog opened");
} else {
console.log("Dialog closed");
}
}
// Open the dialog when the Update button is clicked
updateButton.addEventListener("click", () => {
dialog.showModal();
openCheck(dialog);
});
// Close the dialog when the Cancel button is clicked
cancelButton.addEventListener("click", () => {
dialog.close();
openCheck(dialog);
});
4.2 Attributes and methods of <dialog>
The open attribute specifies whether the dialog box is open. If the attribute is present, the dialog is shown on the page.
Example of using the open attribute:
<dialog open>
<p>This is an open dialog box.</p>
</dialog>
A window shown via the open attribute is always non-modal. To open a modal window, you use the showModal() method (more on it below).
The returnValue property holds a result string: the value the window was closed with. It's a property of the dialog's JavaScript object, not an HTML attribute, so you don't write it in the markup. The value is set by the close('value') method or by a form button with method="dialog" — in that case returnValue gets the value of the button that was clicked. If the window was closed with the Esc key, returnValue doesn't change.
dialog.close('confirm');
console.log(dialog.returnValue); // "confirm"
The show() method:
Opens the dialog box in non-modal mode.
dialog.show();
The showModal() method:
Opens the dialog box in modal mode, blocking interaction with the rest of the page.
dialog.showModal();
The close() method:
Closes the dialog box.
dialog.close();
How to “wire” a method to a button: a minimum of JavaScript
The open attribute shows the window as soon as the page loads. To make the window open and close on a button click, in this lesson we call the show(), showModal() and close() methods from JavaScript — those are exactly what the tasks need. We'll cover the language in detail later; for now, this template is enough:
<button id="btn-open">Show hint</button>
<dialog id="info-dialog">
<p>Window text</p>
<button id="btn-close">Got it</button>
</dialog>
<script>
// 1. Find the page elements by their id
const openBtn = document.getElementById('btn-open');
const closeBtn = document.getElementById('btn-close');
const infoDialog = document.getElementById('info-dialog');
// 2. "When the button is clicked, run the code in the curly braces"
openBtn.addEventListener('click', () => {
infoDialog.showModal(); // open as modal
});
closeBtn.addEventListener('click', () => {
infoDialog.close(); // close
});
</script>
- Put the
<script>at the end of<body>: the browser runs a script as soon as it reaches it, and the elements below the script don't exist yet —getElementByIdwould returnnull. document.getElementById('…')— “give me the element with this id.”element.addEventListener('click', () => { … })— “when the element is clicked, run the code in the braces.” The() => { … }notation is a short function: code that will run later, on click.- For a non-modal window, call
show()instead ofshowModal().
4.3 <dialog> events
The close event:
An event that fires when the dialog box is closed.
<div style="min-height: 200px">
<dialog id="myDialog">
<form method="dialog">
<p>Click "Cancel" or "OK" to return a value</p>
<div class="dialog-buttons">
<button value="cancel">Cancel</button>
<button value="confirm">OK</button>
</div>
</form>
</dialog>
<button id="showDialog">Show dialog</button>
</div>
<dialog id="myDialog">
<form method="dialog">
<p>Click "Cancel" or "OK" to return a value</p>
<div class="dialog-buttons">
<button value="cancel">Cancel</button>
<button value="confirm">OK</button>
</div>
</form>
</dialog>
<button id="showDialog">Show dialog</button>
// Get the DOM elements
const dialog = document.getElementById('myDialog');
const showDialogButton = document.getElementById('showDialog');
// Open the modal window
showDialogButton.addEventListener('click', () => {
dialog.showModal();
});
// Show returnValue when the dialog is closed
dialog.addEventListener('close', () => {
alert(`Dialog closed with return value: ${dialog.returnValue}`);
});
The cancel event:
An event that fires when the dialog box is cancelled (for example, when the Esc key is pressed).
<div style="min-height: 200px">
<dialog id="myDialog">
<form method="dialog">
<p>Press the "Esc" key to see the result</p>
</form>
</dialog>
<button id="showDialog">Open dialog</button>
</div>
<dialog id="myDialog">
<form method="dialog">
<p>Press the "Esc" key to see the result</p>
</form>
</dialog>
<button id="showDialog">Open dialog</button>
// Get the DOM elements
const dialog = document.getElementById('myDialog');
const showDialogButton = document.getElementById('showDialog');
// Open the dialog
showDialogButton.addEventListener("click", () => {
dialog.showModal();
});
// Handle dialog cancellation (Esc key)
dialog.addEventListener('cancel', () => {
alert('Dialog was cancelled');
});
GO TO FULL VERSION