Working with Multiple Images

If you have more than one image on a page, you should give each one a different name. Figure 4-15 has two images and two links. The first link tells JavaScript to swap the image called my_first_image (the sad face) with happy_face.gif. The second link tells JavaScript to swap the image called my_second_image (a circle) with square.gif. The result is shown in Figure 4-16.

Note

When using more than one image, you must name your images differently. If you accidentally give two images the same name, the swap won't work.

JavaScript for swapping two images

Figure 4-15. JavaScript for swapping two images

Note

Image swapping doesn't work in browsers earlier than Internet ...

Get The Book of JavaScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.