Zippy
Zippy

Reputation: 495

Jquery append img and a

I need to append the img inside an <a> tag. I have been hacking at this and no resolve, driving me crazy. If you can help that would be great. I am using jQuery 1.5

while condition{
    $("<img>").attr("src", thumb).appendTo("#images");
}

End result should be:

<a href="#"><img src="xxxxx"></a>

When I use prepend or appendto I get this result:

<div id="images src="xxxxxxx"><a></a> 
OR
<a href="#"></a><img src="xxxxx">

Thanks for any assistance.

Upvotes: 5

Views: 9785

Answers (2)

hunter
hunter

Reputation: 63512

$("#images").append($("<a>", 
{
    href: "#", 
    html: $("<img>", { src: thumb })
}));

working example: http://jsfiddle.net/hunter/Rxkxg/

Upvotes: 8

Brad Christie
Brad Christie

Reputation: 101594

$('<a>') // create anchor first
  .attr('href','#') // set anchor HREF attribute
  .append( // inside it, append an image
    $('<img>') // new image
      .attr('src',thumb) // set image SRC attribute
  ) // end append
  .appendTo('#images') // add to image body

Almost on the right track...

demo

Upvotes: 6

Related Questions