Create new image element using the src of image in another div using jquery

user3014202 picture user3014202 · Mar 20, 2014 · Viewed 8.2k times · Source
<div id='show-image'></div>
<div class='post'>
<div class='inner'>
<img class='post-img' src='http://3.bp.blogspot.com/-Sg5t3utxRzc/UwgyzbLVAAI/AAAAAAAAFBo/vYQX0Cphx8U/s1600/indian-bride.jpg'/>
</div>
</div>

Okay so what I want is that I want to get the src of the image .post-img and want to create a new image element inside the div id='show-image'

I would be glad if anyone can help me to achieve this using jQuery

Answer

Felix picture Felix · Mar 20, 2014

You can use .attr() to get the src of your image then .append() to append newly created image with retrieved src to the div with id show-image:

var src = $('.post-img').attr('src');
$('#show-image').append('<img src="' + src + '" />');

or you can use .appendTo():

var url=$('.post-img').attr("src"); 
$('<img src="'+url+'" />').appendTo('#show-image');