JavaScript Curriculum
Attributes & Dataset
easyEach menu card needs to carry its price, category, and ID so JavaScript can filter, sort, and update them without extra server calls. data-* attributes are the bridge between your HTML and your JS logic.
Attributes & Dataset
Every HTML element carries attributes — class, href, src, disabled, and any data-* you define. JavaScript gives you dedicated methods to read, set, remove, and check them.
Attribute Methods
el.getAttribute('href')Read any attribute by name. Returns null if missing. Works for all attributes including custom ones.
const link = document.querySelector('a')
link.getAttribute('href') // → "/menu"
link.getAttribute('target') // → null (not set)
link.getAttribute('class') // → "logo active"data-* Attributes and dataset
Store any data you need directly on elements — no separate data structures required:
<div class="card" data-id="1" data-price="2.50" data-category="coffee" data-vegan="true"> <h3>Espresso</h3> </div>
const card = document.querySelector('.card')
card.dataset.price
// → "2.50"classList — The Clean Way to Manage Classes
classList gives you dedicated methods for class management — much cleaner than manipulating className as a string:
className: "card featured"// Ready — try the buttons below
Your Challenge
Add data-price and data-category to each .card in your HTML. In JS, use querySelectorAll('.card') and loop through them — read card.dataset.category and log it. Then wire a click handler that uses classList.toggle('selected') on each card.
Challenge
Add data-price, data-category, and data-vegan attributes to your menu cards. Then use dataset to read them in JavaScript. Use classList.toggle to add an 'active' class to a card on click, and classList.contains to check if it is active.
Attributes & Dataset
easyEach menu card needs to carry its price, category, and ID so JavaScript can filter, sort, and update them without extra server calls. data-* attributes are the bridge between your HTML and your JS logic.
Attributes & Dataset
Every HTML element carries attributes — class, href, src, disabled, and any data-* you define. JavaScript gives you dedicated methods to read, set, remove, and check them.
Attribute Methods
el.getAttribute('href')Read any attribute by name. Returns null if missing. Works for all attributes including custom ones.
const link = document.querySelector('a')
link.getAttribute('href') // → "/menu"
link.getAttribute('target') // → null (not set)
link.getAttribute('class') // → "logo active"data-* Attributes and dataset
Store any data you need directly on elements — no separate data structures required:
<div class="card" data-id="1" data-price="2.50" data-category="coffee" data-vegan="true"> <h3>Espresso</h3> </div>
const card = document.querySelector('.card')
card.dataset.price
// → "2.50"classList — The Clean Way to Manage Classes
classList gives you dedicated methods for class management — much cleaner than manipulating className as a string:
className: "card featured"// Ready — try the buttons below
Your Challenge
Add data-price and data-category to each .card in your HTML. In JS, use querySelectorAll('.card') and loop through them — read card.dataset.category and log it. Then wire a click handler that uses classList.toggle('selected') on each card.
Challenge
Add data-price, data-category, and data-vegan attributes to your menu cards. Then use dataset to read them in JavaScript. Use classList.toggle to add an 'active' class to a card on click, and classList.contains to check if it is active.