JavaScript Curriculum
Creating & Removing Elements
easyThe coffee shop menu is now fetched from a server. Instead of hardcoding cards in HTML, JavaScript needs to create them on the fly from data — building each card element, populating it, and inserting it into the page.
Creating & Removing Elements
querySelector finds elements that already exist. createElement builds new ones. This is how every dynamic UI works — fetch some data, build the elements, insert them.
The createElement Workflow
Follow the four steps from element creation to DOM insertion:
const card = document.createElement('div')
// Creates: <div></div>
// Lives in memory only — NOT on the page yetcreateElement creates a new element in memory. It is detached — not visible until you append it to the DOM.
<div></div>
Live DOM Builder
Try adding and removing cards dynamically:
// DOM ready
cloneNode & the template Element
When you need many copies of the same structure, cloning is faster than building from scratch:
Copies the element AND all its descendants. The most common usage.
<div class="card" data-id="1"> <h3>Espresso</h3> <p class="price">£2.50</p> </div>
<div class="card" data-id="1"> <h3>Espresso</h3> ← copied <p class="price">£2.50</p> ← copied </div>
const card = document.querySelector('.card')
const clone = card.cloneNode(true)
// Update clone content before inserting:
clone.dataset.id = '4'
clone.querySelector('h3').textContent = 'Cold Brew'
clone.querySelector('.price').textContent = '£3.50'
document.querySelector('.cards').append(clone)Your Challenge
Write a createCard(name, price) function using createElement. It should return a .card div containing an h3 (name) and a p.price (price). Create three cards and append them all at once using document.createDocumentFragment().
Challenge
Write a createCard(name, price) function that builds a .card element with an h3 and a .price paragraph, and returns it. Then call it three times and append all three cards to .cards using a DocumentFragment in a single DOM insertion.
Creating & Removing Elements
easyThe coffee shop menu is now fetched from a server. Instead of hardcoding cards in HTML, JavaScript needs to create them on the fly from data — building each card element, populating it, and inserting it into the page.
Creating & Removing Elements
querySelector finds elements that already exist. createElement builds new ones. This is how every dynamic UI works — fetch some data, build the elements, insert them.
The createElement Workflow
Follow the four steps from element creation to DOM insertion:
const card = document.createElement('div')
// Creates: <div></div>
// Lives in memory only — NOT on the page yetcreateElement creates a new element in memory. It is detached — not visible until you append it to the DOM.
<div></div>
Live DOM Builder
Try adding and removing cards dynamically:
// DOM ready
cloneNode & the template Element
When you need many copies of the same structure, cloning is faster than building from scratch:
Copies the element AND all its descendants. The most common usage.
<div class="card" data-id="1"> <h3>Espresso</h3> <p class="price">£2.50</p> </div>
<div class="card" data-id="1"> <h3>Espresso</h3> ← copied <p class="price">£2.50</p> ← copied </div>
const card = document.querySelector('.card')
const clone = card.cloneNode(true)
// Update clone content before inserting:
clone.dataset.id = '4'
clone.querySelector('h3').textContent = 'Cold Brew'
clone.querySelector('.price').textContent = '£3.50'
document.querySelector('.cards').append(clone)Your Challenge
Write a createCard(name, price) function using createElement. It should return a .card div containing an h3 (name) and a p.price (price). Create three cards and append them all at once using document.createDocumentFragment().
Challenge
Write a createCard(name, price) function that builds a .card element with an h3 and a .price paragraph, and returns it. Then call it three times and append all three cards to .cards using a DocumentFragment in a single DOM insertion.