I need help for returning the value from the loop. Here is my javascript code :
get_images = ["01","02","03"];
for(var i=0; i < get_images.length; i++){
images = '<img src="images/' + page_id + '/gallery/01/' + get_images[i] +'.jpg" >';
}
$("#imgs").html(images);
What I want is, I want get all the value " images " from inside the loop and put it into inside the
<div id="imgs"></div>
How do I put the images found in the loop into their own div
tag?
Try this..
get_images = ["01","02","03"];
var images ='',
arrLength = get_images.length;
for(var i=0; i < arrLength; i++){
// Here concatenate your values.
images += '<img src="images/' + page_id + '/gallery/01/' + get_images[i] +'.jpg" >';
}
$("#imgs").html(images);
Do you mean to do string append? Right now you're overwriting the images variable in every iteration:
images = images + '<img src="images/' + page_id + '/gallery/01/' + get_images[i] +'.jpg" >';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.