On this page9 sections
Lecture 038: Details, Dialog & Template
| Category | 7: Interactive Elements, Graphics & Media |
| Lecture | 038 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
Some interactive pieces used to need a lot of JavaScript: a section that opens and closes, a pop-up window, a block of HTML you reuse. HTML now has elements for each of them:
<details>,<dialog>and<template>.
Learning Objectives
- Make open/close sections with
<details>and<summary> - Build accordion groups with the
nameattribute - Show modal and non-modal pop-ups with
<dialog> - Store reusable HTML in
<template>and copy it with JavaScript
1. <details> and <summary>
Example: click the summary line:
<details>
<summary>What is your refund policy?</summary>
<p>You can return any item within 14 days for a full refund.</p>
</details>
<details open>
<summary>Do you deliver outside Lahore?</summary>
<p>Yes, to every city in Pakistan.</p>
</details><summary>is the always-visible line; the rest shows when opened.openstarts it expanded.- No JavaScript needed, and it works with the keyboard.
2. Accordions: Only One Open at a Time
Give several <details> the same name. Opening one closes the others.
Example
<details name="faq"><summary>Shipping</summary><p>2–4 working days.</p></details>
<details name="faq"><summary>Payment</summary><p>Card, bank transfer or cash on delivery.</p></details>
<details name="faq"><summary>Returns</summary><p>Within 14 days.</p></details>3. <dialog>: Pop-up Windows
Example
<dialog id="confirm">
<p>Delete this photo?</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</dialog>
<button type="button" onclick="document.getElementById('confirm').showModal()">Delete photo</button>
<p id="result"></p>
<script>
const dlg = document.getElementById('confirm');
dlg.addEventListener('close', () => {
document.getElementById('result').textContent = 'You chose: ' + dlg.returnValue;
});
</script>| Method | Behaviour |
|---|---|
showModal() | Opens on top, dims the page, traps focus, Esc closes it |
show() | Opens without blocking the rest of the page |
close() | Closes it |
A <form method="dialog"> inside closes the dialog, and the clicked button's value becomes returnValue.
Style the dimmed background with CSS ::backdrop (CSS Lecture 047).
4. <template>: Reusable HTML
Content inside <template> isn't shown. JavaScript copies (clones) it whenever needed.
Example
<template id="card">
<div style="border: 1px solid #dee2e6; border-radius: 8px; padding: 10px; margin: 6px 0;">
<strong class="name"></strong> - <span class="city"></span>
</div>
</template>
<div id="list"></div>
<script>
const people = [['Ayesha', 'Karachi'], ['Bilal', 'Multan'], ['Sana', 'Gilgit']];
const tpl = document.getElementById('card');
for (const [name, city] of people) {
const copy = tpl.content.cloneNode(true);
copy.querySelector('.name').textContent = name;
copy.querySelector('.city').textContent = city;
document.getElementById('list').append(copy);
}
</script>Common Beginner Mistakes
1. A clickable <div> to toggle content. <details> does it with better accessibility.
2. Forgetting <summary>. The browser then shows a generic "Details" label.
3. show() when you mean showModal(). Only showModal() blocks the page and supports Esc.
4. Expecting <template> content to appear. It's inert until JavaScript clones it.
Practice Questions
- Which element is always visible inside
<details>? - How do you make a
<details>start open? - How do you build an accordion where only one item is open?
- Which method opens a dialog that blocks the page?
- What does
<form method="dialog">do? - Is the content inside
<template>shown on the page?
Practice Answers
<summary>- Add the
openattribute. - Give every
<details>the samename. showModal()- Submitting it closes the dialog and sets
returnValueto the button's value. - No. It's only shown after JavaScript clones it into the page.
Key Takeaways
<details>+<summary>= an open/close section with no JavaScript.- Same
nameon several<details>= an accordion. <dialog>withshowModal()= an accessible pop-up.<template>holds HTML to clone with JavaScript.