Skip to Main Content
Head Rush Ajax
book

Head Rush Ajax

by Brett McLaughlin
March 2006
Beginner to intermediate content levelBeginner to intermediate
448 pages
13h 33m
English
O'Reilly Media, Inc.
Content preview from Head Rush Ajax
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
you’re on your way 4
253
developing dom apps
I think we should use JavaScript. When we add a CD to the Top
5 listing, we need to remove the event handler on that <img>
element... but then if the user clicks “Start Over”, we need to
move the image back to the “cds” <div>, and then add the handler
back. Seems like it would be easier to just handle adding and
removing the onClick event with JavaScript. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Reinventing the Organization for GenAI and LLMs

Reinventing the Organization for GenAI and LLMs

Ethan Mollick
Head First Ajax

Head First Ajax

Rebecca M. Riordan

Publisher Resources

ISBN: 0596102259Errata PageSupplemental Content