a jQuery image selector

How To Create a Simple Image Selector With jQuery

How to create a simple image selector with jQuery.

Click here to see the demo

The Code:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>A Simple Image Selector</title>
<script type="text/javascript" src="jquery-1.7.2.min.js">
</script>
<script>
$(function() {
$("a:has(img.gallery)").click(function() {
var largePath = $(this).attr("href");
var caption = $(this).attr("title");

$("#photoLarge").attr({src: largePath });
$("#caption").text(caption);
return false;
});
});
</script>
</head>

<body>

<div id="photoGallery">
<div id="photoGroup">
<img id="photoLarge" src="testingPhotos/Desert.jpg" width="500" height="375" />
<p id="caption">Image Caption</p>
</div>
</div>
<div id="footerBar">
<a href="testingPhotos/Desert.jpg" title="A Desert"><img src="testingPhotos/Desert_thumb.jpg" class="gallery" width="128" height="96" alt="desert" /></a>
<a href="testingPhotos/Koala.jpg" title="A Koala"><img src="testingPhotos/Koala_thumb.jpg" class="gallery" width="128" height="96" alt="koala" /></a>
<a href="testingPhotos/Lighthouse.jpg" title="A LightHouse"><img src="testingPhotos/Lighthouse_thumb.jpg" class="gallery" width="128" height="96" alt="light" /></a>
<a href="testingPhotos/Penguins.jpg" title="Group of Penguins"><img src="testingPhotos/Penguins_thumb.jpg" class="gallery" width="128" height="96" alt="pen" /></a>
</div>
</body>
</html>


Leave a Reply

Your email address will not be published. Required fields are marked *