4

I'm making a little gallery in my website and it contains lots of pictures, how can I use a for loop to add the li in my website.

This is my HTML right now and instead of all those <li> I would like a for loop. HTML:

<div id="pictureBlock">
    <div id="three-columns" class="grid-container">
        <ul class="rig columns-3">
            <li><img src="images/1.jpg" /></li>
            <li><img src="images/2.jpg" /></li>
            <li><img src="images/3.jpg" /></li>
            <li><img src="images/4.jpg" /></li>
            <li><img src="images/5.jpg" /></li>
            <li><img src="images/6.jpg" /></li>
            <li><img src="images/7.jpg" /></li>
            <li><img src="images/8.jpg" /></li>
            <li><img src="images/9.jpg" /></li>
            <li><img src="images/10.jpg" /></li>
            <li><img src="images/11.jpg" /></li>
            <li><img src="images/12.jpg" /></li>
            <li><img src="images/13.jpg" /></li>
            <li><img src="images/14.jpg" /></li>
            <li><img src="images/15.jpg" /></li>
        </ul>
    </div>
</div>
3
  • are you getting array of images and want to display them in lis? Commented Apr 14, 2015 at 7:53
  • li length is fixed ? or it depend on some amount of value ? Commented Apr 14, 2015 at 7:53
  • Do you want display all these images using JavaScript. Images do you want to print are hard coded or some back end is provided for displaying images. Commented Apr 14, 2015 at 7:53

8 Answers 8

7

Using jQuery:

var ul = $('ul.rig');
var count = 15; // number of images

for(var i = 1; i <= count; i++) {
    ul.append('<li><img src="images/' + i + '.jpg"></li>');
}
Sign up to request clarification or add additional context in comments.

1 Comment

what's that first line for ?
2

<div id="pictureBlock">
    <div id="three-columns" class="grid-container">
        <ul class="rig columns-3" id="gallery">
        
        </ul>
    </div>
</div>

<script>
    var gal = document.getElementById('gallery');
    

    for (var i = 1; i < 15 ; i++) {
        gal.innerHTML += "<li><img src='images/" + i + ".jpg' /></li>";
    };

</script>

Comments

1

I believe the easiest way would be using jQuery:

var numberOfImages = 15;
var $container = $("#pictureBlock ul");
for (var i=1; i<=numberOfImages; i++) {
  $container.append('<li><img src="images/'+i+'.jpg" /></li>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pictureBlock">
    <div id="three-columns" class="grid-container">
        <ul class="rig columns-3"></ul>
    </div>
</div>

Comments

1

01 - If your image file name are ordered by number and are in same location

// Define number of images
var numImages = 15;

// Get the div you want to add element to
var rigDiv = $(".rig");

// Loop through images and add them to target div
for (var i=1; i <= numImages; i++) {
    var element = '<li><img src="images/' + i + '.jpg" /></li>';
    rigDiv.append(element);
}

02 - If your image file name are not in order and are not in same directory

// Define images array
var images = [
  'images/1.jpg',
  'images/2.jpg',
  ....
  'images/10.jpg'
];

// Get the div you want to add element to
var rigDiv = $(".rig");

// Loop through images and add them to target div
for(var image of images){
    var element = '<li><img src="' + image + '" /></li>';
    rigDiv.append(element);
}

Comments

1

Simply you can use a for loop,

for (var i = 1; i < 16; i++) {
  $('ul.rig').append('<li><img src="images/' + i + '.jpg" /></li>')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="pictureBlock">
  <div id="three-columns" class="grid-container">
    <ul class="rig columns-3">
    </ul>
  </div>
</div>

Comments

0

Using jQuery you could do something like in this Fiddle i just created:

var pictures = [ "1", "2" ];

$.each(pictures, function(value) {
    $(".rig").append("<li><img src='images/" + value + ".jpg' /></li>");
});

http://jsfiddle.net/wd09ccpa/

In the var pictures would have to be some list of the pictures you have.

Comments

0

Jquery for the win:

for(var i = 1; i <= count; i++) {
   $('ul.rig').append("<li><img src='images/'+i+'.jpg'></li>");
}

Comments

0
var count = 15;
for(let i=1; i<=count; i++){
    $("ul").append('<li><img src="images/'+ i +'.jpg" /></li>');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.