Reputation: 495
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
Reputation: 63512
$("#images").append($("<a>",
{
href: "#",
html: $("<img>", { src: thumb })
}));
working example: http://jsfiddle.net/hunter/Rxkxg/
Upvotes: 8
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...
Upvotes: 6