From 69cf93eb72e84caf46aed15e4f783e355faf75d1 Mon Sep 17 00:00:00 2001 From: Clarence Bakosi Date: Mon, 3 Nov 2025 20:43:45 +0100 Subject: [PATCH] feat(curriculum): Add interactive examples to dialog element lesson (#63477) --- .../673369829e232835c2732656.md | 77 +++++++++++++------ 1 file changed, 55 insertions(+), 22 deletions(-) diff --git a/curriculum/challenges/english/blocks/lecture-working-with-the-dom-click-events-and-web-apis/673369829e232835c2732656.md b/curriculum/challenges/english/blocks/lecture-working-with-the-dom-click-events-and-web-apis/673369829e232835c2732656.md index 154dc103cd9..79ec4b47430 100644 --- a/curriculum/challenges/english/blocks/lecture-working-with-the-dom-click-events-and-web-apis/673369829e232835c2732656.md +++ b/curriculum/challenges/english/blocks/lecture-working-with-the-dom-click-events-and-web-apis/673369829e232835c2732656.md @@ -5,7 +5,7 @@ challengeType: 19 dashedName: how-do-you-open-and-close-dialog-elements-using-javascript --- -# --description-- +# --interactive-- Dialogs let you display important information or actions to users. With HTML's built-in `dialog` element, you can easily create these dialogs (both modal and non-modal dialogs) in your web apps. @@ -19,68 +19,99 @@ When you want to make sure the user focuses on a specific action or message of a Here's the HTML for the modal dialog: +:::interactive_editor + ```html -

This is a modal dialog.

+

This is a modal dialog.

``` +::: + For now, you can't see anything on the page because the modal is closed on the initial render. You can automatically open the modal by using the `showModal()` method: +:::interactive_editor + +```html + +

This is a modal dialog.

+
+ +``` + ```js -const dialog = document.getElementById("my-modal"); +const dialog = document.getElementById("modal"); dialog.showModal(); ``` +::: + The result in the browser will show a modal with the text `This is a modal dialog.` It's best to give control to the user. To achieve this, you can add a click event listener to a button and use the `showModal()` method: +:::interactive_editor + ```html - -

This is a modal dialog.

+ +

This is a modal dialog.

- + + ``` -Here's the JavaScript: - ```js -const dialog = document.getElementById("my-modal"); -const openButton = document.getElementById("open-modal"); +const dialog = document.getElementById("modal"); +const openButton = document.getElementById("open-modal-btn"); openButton.addEventListener("click", () => { dialog.showModal(); }); ``` +::: + If you needed to show a dialog while still allowing interaction with content outside of the dialog, then you can use the `show()` method: +:::interactive_editor + +```html + +

This is a modal dialog.

+
+ + +``` + ```js -const dialog = document.getElementById("my-modal"); -const openButton = document.getElementById("open-modal"); +const dialog = document.getElementById("modal"); +const openButton = document.getElementById("open-modal-btn"); openButton.addEventListener("click", () => { dialog.show(); }); ``` -To close a modal, you can add a button to the modal inside the `dialog` element: +::: + +To close a modal, you can add a button to the modal inside the `dialog` element and use the `close()` method: + +:::interactive_editor ```html - -

This is a modal dialog.

- + +

This is a modal dialog.

+
- + + ``` -Then use the `close()` method on the button: - ```js -const dialog = document.getElementById("my-modal"); -const openButton = document.getElementById("open-modal"); -const closeButton = document.getElementById("close-modal"); +const dialog = document.getElementById("modal"); +const openButton = document.getElementById("open-modal-btn"); +const closeButton = document.getElementById("close-modal-btn"); openButton.addEventListener("click", () => { dialog.show(); @@ -91,6 +122,8 @@ closeButton.addEventListener("click", () => { }); ``` +::: + # --questions-- ## --text--