Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Ask Before Deleting With a Dialog

Sam’s list removed an item the moment its Remove button was pressed, so one slip lost the basmati rice for good. In this video, you made Remove ask first with a dialog, watched the page wait while the question was open, and acted only on the answer Remove. Here are the key points:

  • The browser’s confirm() box lets you choose only its message, not its buttons or its look, and a box you build yourself on top of the page leaves the focus and the Escape key to you. A <dialog> element gives you your own buttons and handles the focus and Escape for you.
  • A <dialog> stays hidden until your code opens it. Its aria-labelledby names it after the question’s paragraph, so screen readers announce the question.
  • Submitting a form with method="dialog" inside the dialog closes the dialog instead of sending anything. The form’s buttons send the form, as any button in a form does unless its type says otherwise, and each carries a value, here keep or remove.
  • showModal() doesn’t wait for the answer. Unlike confirm(), it returns at once, so the removing waits for the dialog to close, in a listener of its own. That listener won’t know which Remove was pressed, so the click listener remembers the button in pendingButton, outside both listeners.
  • showModal() opens the dialog as a modal, so the user has to answer before going back to the page. The focus moves into the dialog, onto its first button, Keep it, so Enter right away is the safe answer. A light gray backdrop covers the page. Clicking Remove eggs does nothing, and Tab moves to Remove, then out to the browser’s toolbar, never to the list. The page behind is inert.
  • When the dialog closes, it fires a close event, and the dialog’s returnValue holds the value of the button that closed it. Keep it gives "keep".
  • Escape closes the dialog too, with no button. Firefox and Safari then leave returnValue as it was, even "remove" from an earlier answer, so Escape would remove the next item too. The click listener therefore sets it to an empty string before showModal(). Escape then leaves that empty string, and nothing is removed.
  • Watch out: show(), or an open attribute you write on the dialog yourself, displays it too, but not as a modal. The page behind stays clickable, and Escape does nothing. showModal() sets open for you, so always open the dialog with it.
  • The browser returns the focus to the button that opened the dialog, before the close event fires. After a removal, your code moves it on to the neighbor’s Remove button, as before.

In kitchen/index.html, the dialog goes right after the list:

Language: HTML Filename: kitchen/index.html
<ul id="items"></ul>
<dialog id="confirm-remove" aria-labelledby="confirm-text">
  <form method="dialog">
    <p id="confirm-text"></p>
    <button value="keep">Keep it</button>
    <button value="remove">Remove</button>
  </form>
</dialog>

In kitchen/main.js, two lookups and a variable for the button that opened the dialog go next to the others:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Sam’s list removes an item the moment you press its button. Press Remove basmati rice by mistake, and the rice is gone, with no way back. So Remove should ask first.

00:12 The browser’s built-in confirm box could ask, but you choose only its message, never its buttons or its look. A box you build yourself leaves the focus and Escape to you.

00:23 HTML’s dialog element gives you your own buttons and handles the focus and Escape for you.

00:30 In index.html, add a dialog element after the list. It stays hidden until your code opens it. Its empty paragraph will hold the question. The dialog’s aria-labelledby names it after that paragraph, so screen readers announce the question.

00:47 The paragraph sits in a form with method=”dialog”. Submitting that form closes the dialog instead of sending anything. Its two buttons send the form, as any button in a form does unless its type says otherwise.

01:01 Each one carries a value, keep or remove.

01:06 In main.js, the list’s click listener keeps its first lines, which find the Remove button. Then, instead of removing, it puts the button’s words and a question mark in the paragraph.

01:17 It calls showModal to open the dialog.

01:21 The confirm box pauses your code until it’s answered. showModal doesn’t wait. It returns at once. So the removing has to wait for the dialog to close, in a listener of its own.

01:33 That listener won’t know which Remove was pressed, so this one remembers the button outside both listeners.

01:40 Save, add the three items again, and press Enter on Remove basmati rice. The dialog opens and asks, Remove basmati rice?

01:51 The dialog opens with the focus on its first button, Keep it, so Enter right away is the safe answer.

01:59 Behind it, a light gray backdrop covers the page. Clicking Remove eggs does nothing. Press Tab, and the focus moves to Remove. Again, and it leaves for the browser’s toolbar, never the list.

02:13 The page behind is inert: no clicks, no focus. That’s what modal means. The user has to answer before going back to the page.

02:24 When the dialog closes, it fires a close event. The dialog’s returnValue then holds the value of the button that closed it. Press Enter on Keep it. The dialog closes, returnValue is keep, and the rice stays.

02:39 Your turn: pause the video and predict where the focus went. It’s back on Remove basmati rice, the button that opened the dialog. The browser returns it there for you.

02:52 Escape closes the dialog too, with no button. Firefox and Safari keep the old returnValue, so after one removal, Escape would remove the next item too. Before showModal, the click listener sets returnValue to an empty string.

03:08 Open it again and press Escape. The dialog closes, and returnValue is that empty string. Nothing is removed. Watch out: the dialog’s show method, or an open attribute you write on it yourself, displays it too, but not as a modal.

03:25 The page behind stays clickable, and Escape does nothing. Always open it with showModal.

03:33 So the removing moves into a close listener on the dialog. If returnValue isn’t remove, it returns. Otherwise it runs your removal code from before, on the remembered button.

03:45 Open the dialog once more, Tab to Remove, and press Enter. The rice is gone, and the status line says basmati rice removed. The browser put the focus back on the opener first, and your code moved it on to Remove fresh basil.

04:02 To ask before acting, open a dialog with showModal, close it with a form with method=”dialog”, and read the dialog’s returnValue when it closes. The browser handles the focus and the Escape key for you.

04:16 Sam’s list now asks before it removes anything. Next, in Open Panels With Popovers and Details, you’ll show and hide panels that don’t need an answer.

Become a Member to join the conversation.