JavaScript Curriculum
Selecting Elements
easyThe coffee shop menu is in the DOM. Now we need to grab specific elements — the order button, all the price tags, the navigation links — so we can make them interactive.
Selecting Elements
The DOM is a tree of nodes. Your first job is finding the right ones. JavaScript gives you five selection methods — pick the right tool for each situation.
The Five Selection Methods
<selectorplayground></selectorplayground>
Live Query Sandbox
Try selecting different elements from the coffee shop DOM:
[0] <div class="card" data-price="2.50">…</div>
[1] <div class="card" data-price="3.50">…</div>
[2] <div class="card featured" data-price="4.00">…</div>
Test Your Selector Knowledge
<div id="app"> <p class="intro highlight">Welcome</p> </div>
What's the best way to select: #app p?
Your Challenge
Open the coffee shop page. In the console, select the first .card, log its textContent. Then querySelectorAll('.card') and log .length. Then add id="main-nav" to your nav and select it with getElementById.
Challenge
Select the first .card with querySelector and log its textContent. Then use querySelectorAll to select all .card elements and log how many there are. Finally, use getElementById to grab an element by ID and change its textContent.
Selecting Elements
easyThe coffee shop menu is in the DOM. Now we need to grab specific elements — the order button, all the price tags, the navigation links — so we can make them interactive.
Selecting Elements
The DOM is a tree of nodes. Your first job is finding the right ones. JavaScript gives you five selection methods — pick the right tool for each situation.
The Five Selection Methods
<selectorplayground></selectorplayground>
Live Query Sandbox
Try selecting different elements from the coffee shop DOM:
[0] <div class="card" data-price="2.50">…</div>
[1] <div class="card" data-price="3.50">…</div>
[2] <div class="card featured" data-price="4.00">…</div>
Test Your Selector Knowledge
<div id="app"> <p class="intro highlight">Welcome</p> </div>
What's the best way to select: #app p?
Your Challenge
Open the coffee shop page. In the console, select the first .card, log its textContent. Then querySelectorAll('.card') and log .length. Then add id="main-nav" to your nav and select it with getElementById.
Challenge
Select the first .card with querySelector and log its textContent. Then use querySelectorAll to select all .card elements and log how many there are. Finally, use getElementById to grab an element by ID and change its textContent.