252
what’s the goal
Chapter 4.5
Use JavaScript to programmatically add
event handlers to all <img> elements.
Option 2:
<img class=”cover”
src=”images/shepherd_ledbetter.jpg”
alt=”Ledbetter Heights”
onClick=”addToTop5();” />
<img class=”cover”
src=”images/johnson_complete.jpg”
alt=”The Complete Recordings”
onClick=”addToTop5();” />
<img class=”cover”
This time, there’s nothing in the
HTML that sets up an event
handler for the CD covers.
img
img
img
img
img
div
Here’s part of the
DOM for the Top 5
web page. This is the
“cds” <div>...
...and here are all the CD
covers, represented as
<img> elements.
NEED ARROWS
Using JavaScript, we need to
set up each <img> element to
run addToTop5() when the
<img> is clicked on.
function addToTop5() {
// Add a CD to the Top 5 list
}
function startOver() {
// Remove all CDs from the Top 5 and start over
}
top5.js
Add CD rankings
Create JS le
Add CD to Top 5
Start over