Amen Ra
Amen Ra

Reputation: 2851

Trying to create images with an array of image src attributes using jquery

I have a n array of imag src attributes from images that I was able to push to an array:

var imgSrcArray = Array();
$('.ito-image').each(function(){
     imgSrcArray.push($(this).attr('src'));
});

I want to take these attributes now and create new images for a different purpose on my webpage how can I do this. And is this he best practice?

Upvotes: 0

Views: 378

Answers (2)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

you can iterate same way using $.each() and append to some container div:

$(imgSrcArray).each(function (index, item) {

    $("#new").append('<img src="' + item + '"/>');

});

or you can directly do it:

$('.ito-image').each(function(){
     $("#new").append('<img src="' + $(this).attr("src") + '"/>');
});

FIDDLE:

http://jsfiddle.net/ehsansajjad465/0oh6mma0/

Upvotes: 2

Kodr.F
Kodr.F

Reputation: 14390

is that what u want ?

for (var i=0; i < imgSrcArray.length; i++){

  $('body').append("<img src='"+ imgSrcArray[i] +"' />");

}

Upvotes: 2

Related Questions