Modal/Lightbox in JavaScript
Open lab rules in JavaScript with dialog.showModal and close. Check backdrop clicks against the dialog bounds; restore focus to the opening button on close.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<button id="open">Read lab rules</button><dialog id="rules"><h1>Lab rules</h1><p>Keep food away from the computers.</p><button id="close">Close</button></dialog>7<script>8const dialog = document.querySelector('#rules');9const open = document.querySelector('#open');10open.onclick = () => { if (!dialog.open) dialog.showModal(); };11document.querySelector('#close').onclick = () => dialog.close();12dialog.addEventListener('click', event => {13 if (event.target !== dialog) return;14 const rect = dialog.getBoundingClientRect();15 if (event.clientX < rect.left || event.clientX > rect.right || event.clientY < rect.top || event.clientY > rect.bottom) dialog.close();16});17dialog.addEventListener('close', () => open.focus());18</script>19</body>20</html>
Sample Output
Keep food away from the computers.
The native modal dialog displays this text above the page, with a backdrop behind it.
In this one
- Open a native modal dialog
- Close it with a visible control
- Recognize a backdrop click
- Return keyboard focus after closing
How it works
- 1
Put the heading, rules, and Close button inside dialog.
- 2
Call showModal only when it is not already open.
- 3
Connect the Close button to dialog.close.
- 4
Compare click coordinates with the dialog rectangle for backdrop dismissal.
- 5
After closing, return focus to Read lab rules.
Edge cases
Repeated open request
Calling showModal on an unsuitable state can fail.
The example checks dialog.open before opening. Keep a single clear owner of the dialog state.
Click inside the box
Clicking dialog padding also targets the dialog element.
Check the coordinates as well as event.target, or leave backdrop dismissal out and rely on Close and Escape.
What's going on
A modal is a small window that asks the person to deal with it before returning to the rest of the page. A lightbox is a similar overlay often used for an enlarged image. This example uses a native dialog to show lab rules without making a new page.
showModal opens the dialog above the page and lets the browser manage modal focus behavior. Focus is the element that receives keyboard input. The Close button calls close, and supported browsers also let Escape dismiss a modal dialog. The open check avoids asking an already-open dialog to open again.
The backdrop-click handler checks the dialog's rectangle so a click inside its empty padding is not mistaken for a click outside. When it closes, the code returns focus to the opening button. Use a short heading and a clear close action. Native dialog handles much of the behavior that a hand-made overlay would otherwise need to implement and test.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which method creates the modal state?
Pick an answer to check it.
Question 2
Where does focus return on close?
Pick an answer to check it.
Questions people ask
Why use dialog instead of a hidden div?
A native dialog provides modal behavior and keyboard handling that a plain div does not have.
Will clicking the rule text close it?
No. The outside-coordinate check closes only clicks beyond the dialog box.
Why return focus to the opener?
It gives keyboard users a clear place to continue after the dialog disappears.