Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to loop / increment html code with values up to 55

I need to create a list of links in a static (.html) page automatically, is there a way of doing this using JavaScript. I tired a few scripts but couldn't work it out.

this is my html...

<a href="1.html"><img src="images/1.jpg" width="119" height="121" alt="" /></a>

and this is what i want the script to generate 55 times...

<a href="1.html"><img src="images/1.jpg" width="119" height="121" alt="" /></a>
<a href="2.html"><img src="images/2.jpg" width="119" height="121" alt="" /></a>
<a href="3.html"><img src="images/3.jpg" width="119" height="121" alt="" /></a>

... and so on, call me lazy but any help would be most appreciated :)

like image 209
Amesey Avatar asked Sep 15 '26 07:09

Amesey


1 Answers

This should do the trick:

<div id="links">
</div>

<script>
var strLinks = '';

for (var i = 1; i <= 55; i++) {
    strLinks += '<a href="'+ i +'.html"><img src="images/'+ i +'.jpg" width="119" height="121" alt="" /></a>';
}

document.getElementById("links").innerHTML = strLinks;
</script>

JS Fiddle example: http://jsfiddle.net/RWUdG/2/

EDIT: Oops, missed a quote. Fixed.

like image 74
Adam Plocher Avatar answered Sep 17 '26 19:09

Adam Plocher