Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 529
Zippy Avatar asked May 25 '11 19:05

Zippy


People also ask

How to append images in jQuery?

With jQuery, you can dynamically create a new image element and append it at the end of the DOM container using the . append() method.


1 Answers

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

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

like image 89
hunter Avatar answered Oct 02 '22 06:10

hunter