views:

312

answers:

3

This is driving me nuts! What the easiest way to wrap an anchor with the img src as the href in this code?:

$(function(){
   $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=tree&tagmode=any&format=json&jsoncallback=?", 
       function(data){
       $.each(data.items, function(i, item){
        $("<img/>").attr("src", item.media.m).appendTo("#images");

            if (i == 3) 
                       return false;    
   }); 
 }); 
});

It would be great if the outputted HTML code could look something like this:

<div id="images">
    <a href="...888_m.jpg"><img src="...888_m.jpg"></a>
    <a href="...373_m.jpg"><img src="...373_m.jpg"></a>
    <a href="...a17_m.jpg"><img src="...a17_m.jpg"></a>
    <a href="...c51_m.jpg"><img src="...c51_m.jpg"></a>
</div>

Here is what I've come up with so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
    <head>
        <meta name="generator" content="HTML Tidy, see www.w3.org">
        <title>JSON Example</title>
        <script type="text/javascript" src= "http://code.jquery.com/jquery-latest.js"&gt;
        </script>

        <script type="text/javascript">
            $(function(){
                $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=tree&amp;tagmode=any&amp;format=json&amp;jsoncallback=?", 
                    function(data){
                    $.each(data.items, function(i, item){

                        $("<img/>").attr("src", item.media.m).appendTo("#images"); 

                    if (i == 3) 
                            return false; //return 4 images then stop   


                });     

                var imgs = document.getElementsByTagName("img"); //build imgs array

                for (var i=0; i<imgs.length; i++) {
                    source = imgs[i].getAttribute("src"); // grabs the img src attributes 
                    //build anchors with attributes href and title
                    $("<a>").attr({ 
                        href: source,
                        title: "Courtesy of Flicker"
                    }).appendTo("#images"); //injects anchors into "images" div

                    /**********************
                     then I'm stuck. Although everything so far is working, 
                     I need to rewrite the code inserted into the DOM at this point
                     so that the images are wrapped by the anchors. 

                    **********************/

                };
            });

        });
        </script>
        <style type="text/css">
            img {
                height: 100px;
            }
        </style>
    </head>
    <body>
        <div id="images">  </div>
    </body>
</html>

Any ideas?

+1  A: 

Why does it always have to be a one-liner?

$.each(data.items, function(i, item){
    var image = $("<img/>").attr("src", item.media.m);
    var link = $("<a>").attr("href", item.media.url);
    link.append(image);
    $("#images").append(link);
}
Anurag
+1 for being quicker
munch
thanks. i still wonder how a jquery question went unanswered for 9 full minutes.
Anurag
haha. everyone else is at dinner, i guess?
munch
you guys rock! Both answers are great. I also included them in my examples since they gave me much better insight on how to use jQuery. Thanks to you both.
Alfonse
+1  A: 
$.each(data.items, function(i, item){
   var img = $("<img/>").attr("src", item.media.m);
   $("<a/>").attr({href: item.media.m, title: "Courtesy of Flicker"})
      .append(img).appendTo("#images");
});
munch
+1  A: 

Wow! That was quick guys! I actually answered my own question after I asked it. A one liner. Here's what I came up with:

$.each(data.items, function(i, item){
 $("<img/>").attr("src", item.media.m).appendTo("#images").wrap($("<a/>").attr("href", item.media.m));

});

Thanks. Unless there are performance issues I'm not aware of, I'll use my answer.

Alfonse