दिन 11: details, dialog, progress और data-*
✅ आज आप सीखेंगे
- details/summary से FAQ accordions (शून्य JS)
- dialog और popover से popups
- progress, meter, output और mark
- template से दोबारा इस्तेमाल होने वाला markup
- data-* attributes में अतिरिक्त data
🏢 असली दुनिया में
Sharma Sweets को एक FAQ section (“क्या यह eggless है?”, “क्या delivery करते हैं?”), Diwali offer popup, bulk orders के लिए delivery-progress bar और price व weight याद रखने वाले product cards चाहिए। कई developers बड़ी JavaScript library उठा लेते हैं – आधुनिक HTML ज़्यादातर काम ख़ुद कर देता है।
details और summary: शून्य JavaScript वाला accordion
<style>details{border:1px solid #fcd9c8;border-radius:8px;padding:8px 12px;margin:6px 0;font-family:system-ui}
summary{cursor:pointer;font-weight:600;color:#9a3412}</style>
<details open>
<summary>Are your sweets eggless?</summary>
<p>Yes. All our mithai is 100% vegetarian and eggless, made in pure desi ghee.</p>
</details>
<details name="faq">
<summary>Do you deliver outside Jaipur?</summary>
<p>We ship dry sweets (kaju katli, soan papdi) across India in 3–5 days.</p>
</details>
<details name="faq">
<summary>How long do they stay fresh?</summary>
<p>Milk sweets: 2 days in the fridge. Dry sweets: up to 15 days.</p>
</details>कई <details> को एक ही name दें (ऊपर के आख़िरी दो की तरह) तो browser एक समय में सिर्फ़ एक खुला रखता है – सभी आधुनिक browsers में चलता है।
dialog: सही popups
<style>dialog{border:none;border-radius:12px;padding:20px;max-width:280px;font-family:system-ui;box-shadow:0 10px 40px #0004}
dialog::backdrop{background:#0006}</style>
<button type="button" onclick="offer.showModal()">🎁 See Diwali offer</button>
<dialog id="offer" aria-labelledby="ot">
<h2 id="ot">Diwali Special 🪔</h2>
<p>Flat 10% off on gift boxes above ₹2,000 till 1 November.</p>
<form method="dialog"><button>Close</button></form>
</dialog>| Feature | मुफ़्त में क्या मिलता है |
|---|---|
| showModal() | काला backdrop, पीछे का page inactive, focus अंदर |
| Esc key | Dialog बंद |
| <form method="dialog"> | अंदर के buttons बिना JS logic बंद करते हैं |
| ::backdrop | धुँधले background को CSS से style करें |
popover: no JavaScript at all
<button popovertarget="info">ℹ️ Ingredients</button> <div id="info" popover style="padding:12px;border-radius:8px;font-family:system-ui"> Cashew 60%, sugar 38%, ghee 2%, silver vark. </div>
progress, meter, output, mark
<div style="font-family:system-ui"> <p>Bulk order status: <progress value="3" max="5">3 of 5</progress> 3 / 5 steps (packing)</p> <p>Uploading photo: <progress>loading…</progress> (no value = busy)</p> <p>Box filled: <meter min="0" max="1" low="0.3" high="0.8" optimum="1" value="0.65">65%</meter> 65%</p> <p>Shop rating: <meter min="0" max="5" value="4.6">4.6/5</meter> 4.6 / 5</p> <form oninput="total.value = (+kg.value) * 900"> <label>Kaju katli kg <input id="kg" type="number" value="2" min="1" style="width:60px"></label> = ₹<output name="total" for="kg">1800</output> </form> <p>Search result: Fresh <mark>ghewar</mark> available today.</p> </div>
progress
- समय के साथ काम पूरा होना
- Order steps, uploads, quiz progress
- value नहीं = अनिश्चित spinner
meter
- ज्ञात range में माप
- Rating, storage, stock level
- low/high/optimum रंग बदलते हैं
data-* attributes और template
<style>.card{display:inline-block;border:1px solid #ddd;border-radius:8px;padding:8px;margin:4px;font-family:system-ui;cursor:pointer}
.card[data-stock="low"]{border-color:#e34f26}</style>
<div class="card" data-name="Kaju Katli" data-price="900" data-stock="low">Kaju Katli</div>
<div class="card" data-name="Ghewar" data-price="650" data-stock="ok">Ghewar</div>
<p id="out">Click a card…</p>
<script>
document.querySelectorAll('.card').forEach(function (c) {
c.onclick = function () { out.textContent = c.dataset.name + ': ₹' + c.dataset.price + '/kg, stock ' + c.dataset.stock; };
});
</script><ul id="list" style="font-family:system-ui"></ul>
<template id="row"><li><b class="n"></b> – ₹<span class="p"></span>/kg</li></template>
<script>
var sweets = [['Ladoo', 500], ['Rasmalai', 700], ['Ghewar', 650]];
sweets.forEach(function (s) {
var li = row.content.cloneNode(true);
li.querySelector('.n').textContent = s[0];
li.querySelector('.p').textContent = s[1];
list.appendChild(li);
});
</script>data-* और template, HTML और JavaScript के बीच पुल हैं। JavaScript course में आप इन्हीं से पूरी Sharma Sweets cart बनाएँगे।
🏅 आज के Job Skills
Practice tasks
🏋️ Practice – ख़ुद करें
Solution देखें
<details name="d" open><summary>Minimum order?</summary><p>₹500 within 5 km.</p></details> <details name="d"><summary>Delivery time?</summary><p>Within 2 hours, 9 am – 9 pm.</p></details> <details name="d"><summary>Payment?</summary><p>UPI, cards or cash on delivery.</p></details>
<meter> दिखाएँ: 10 में से 7 visits पूरे।Quick quiz
🧠 Quick Quiz – ख़ुद को जाँचें
Interview corner
💼 Interview Corner
div वाले modal की जगह <dialog> क्यों?
showModal() वाला dialog सही role, focus प्रबंधन, Esc से बंद, inactive background और ::backdrop देता है – divs से हाथ से बनाने पर ये सब आसानी से ग़लत हो जाते हैं।
Data attributes किसलिए?
Elements पर custom, न दिखने वाला data (IDs, prices, states) रखने के लिए, जिसे JavaScript dataset से पढ़ता है और CSS attribute selectors से target करता है – बिना invalid attributes बनाए।
सारांश
- details/summary = accordion; एक name = एक ही खुला।
- dialog.showModal() और popover accessible popups देते हैं।
- Tasks के लिए progress, माप के लिए meter, नतीजों के लिए output।
- data-* अतिरिक्त data रखता है; template दोबारा उपयोग वाला markup।
Day 12: graphics और embeds – inline SVG icons, canvas drawing, और iframe से Google Maps व YouTube को सुरक्षित embed करना।
