O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a Dynamic Image Gallery

var img = $("<img></img>"); img.attr("src", "images/newImage.jpg"); $("div").append(img);

Images are becoming a mainstay in web pages. They deliver much more content, and they accomplish it more quickly and often more accurately than text. jQuery and JavaScript allow you to add images to a web page dynamically on demand by creating image elements, then setting the source attribute of each and then appending them to the correct location.

For example, the following code creates an <img> element, sets the src attribute to define the file location, and then appends the image to a <div> element:

var img = $("<img></img>"); img.attr("src", "images/newImage.jpg"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required