CodeGym /Courses /Frontend SELF EN /Elements for Dialog Markup

Elements for Dialog Markup

Frontend SELF EN
Level 10, Lesson 4
Available

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:

HTML
    
      <dialog>
        <!-- Dialog box content -->
      </dialog>
    
  
HTML
    
      <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>
    
  
HTML
    
      <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>
    
  
JavaScript
    
// 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:

HTML
    
      <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.

JavaScript
    
dialog.close('confirm');
console.log(dialog.returnValue); // "confirm"
    
  

The show() method:

Opens the dialog box in non-modal mode.

JavaScript
    
      dialog.show();
    
  

The showModal() method:

Opens the dialog box in modal mode, blocking interaction with the rest of the page.

JavaScript
    
      dialog.showModal();
    
  

The close() method:

Closes the dialog box.

JavaScript
    
      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:

HTML
    
<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 — getElementById would return null.
  • 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 of showModal().

4.3 <dialog> events

The close event:

An event that fires when the dialog box is closed.

HTML
    
      <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>
    
  
HTML
    
      <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>
    
  
JavaScript
    
// 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).

HTML
    
      <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>
    
  
HTML
    
      <dialog id="myDialog">
        <form method="dialog">
          <p>Press the "Esc" key to see the result</p>
        </form>
      </dialog>
      <button id="showDialog">Open dialog</button>
    
  
JavaScript
    
// 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');
});
    
  
1
Task
Frontend SELF EN, level 10, lesson 4
Locked
Simple Dialog Box
Simple Dialog Box
1
Task
Frontend SELF EN, level 10, lesson 4
Locked
Attributes of
Attributes of
Comments (3)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Mirko Mirxas Level 6
8 December 2025
These tasks are basically about JS interactions with the Dialog element. I don't see the relevance here. I think it is completely unrelated to the lesson
Andy Level 11, Deutschland
25 November 2025
I miss the explanation for the "dialog" part, with the javascript (css) elements.
ceciliabarranco94 Level 19, CDMX, Mexico
7 March 2025
not very css