views:

51

answers:

3

Hi people!

Interested to know if I can use jQuery to find the first image on the page and display it inside a resized to a thumbnail with click to enlarge. For example, on this particular blog, when the client adds an image to the post, jQuery will find the first image, display it as a clickable thumbnail in a particular location. It would be ideal if the detailed image could display in thickbox, facebox or another jQuery overlay window.

Thanks in advance for your ideas. Jackson

+2  A: 

You can use the :first selector to find the first image, then using cloning you can copy the image to any other location and modify it from there.

$(document).ready(function() {

    var firstImage = $('img:first');
    var link = $('<a />')
                    .attr('href', '#')
                    .append($('img:first').clone().css({ width:100, height:100 }))
                    .append($('<div />').text('Click to Enlarge'))
                    .click(function(e) {
                        e.preventDefault();
                        // showDialog();
                    });
    $('.imageThumbnail').append(link);

    $('.imageDialog').append(firstImage.clone());

});
bendewey
Wouldn't cloning be somewhat of an overkill (especially when done twice) when all that's being persisted is the image's src?
Ariel
you're only cloning the HTML. IMHO the impact would be minimal
bendewey
+1  A: 

To find the first image in your page you can use the :first selector.

$("img:first") will select the first img element.

If you want to restrict the search within a parent element then you can

$("#parentElemId").find("img:first")

If you can give these image elements a class name then you can do

$("img.classname:first")
rahul
when restricting using the second option why not use `#parentElemId img:first` or `#parentElemId > img:first`?
bendewey
Good point bendeway. $("#parentid img:first") is much cleaner.
Ariel
The method that @pulse provided shows a useful optimization if you plan on doing something else with the parent node. For example, `var n = $("#parentElemId"); ... n.find("img:first");`.
Justin Johnson
+1  A: 

Like other members have mentioned you can get the first image by using img:first. The simplest way to display it as a thumbnail would be:

$(document).append("<img src='" + $("img:first").attr("src") + "' alt='' class='thickbox' style='width: 50px; height: 50px;' />");

The class='thickbox' part is for the thickbox plugin

Here's a more specific sample based on your comment:

If the original data looks something like this:

<div id="thumbs"></div>
<div id="article">
    <img src="..." alt="" />
    Some text..
</div>

You can use something like this:

var images = "";

$("#article img:first").each(function() {
    images += "<img src='" + $(this).attr('src') + "' width='45' class='thickbox' alt='Click to enlarge' /> ";
});

$("#thumbs").append(images);
Ariel
Sorry for my ignorance but I can't see from your code where the image would be appended to?
Jackson
In the sample the image would simply be appended to the document meaning it'll appear as the first item. You can easily change that by replacing document with any valid selector for the parent element
Ariel
Thanks Ariel. I will be using this on a blog with several article intro's. Each article intro will contain an image: <div class="intro"><div class="thumbcontainer"></div><img src="bigimage.jpg">Content here</div>. Being new to jQuery it is hard for me to see how to I would use your code to find the images in each intro on the page and append to the .thumbcontainer of each intro so each intro has a thumbnail. Thanks in advance for your help.
Jackson
@Jackson - I've updated the answer with a more specific sample based on your comment. I'm assuming there's only one thumbcontainer for the whole document and not one for each, that's why I included a sample of what the actual source should look like...
Ariel