I have an un-ordered HTML list of images. Each Line (li) element has its own id:
<ul id="Demo3">
<li id="li1"><img src="images/FT/HopkinsPrairie.jpg" /></li>
<li id="li2"><img src="images/FT/PineForest.jpg" /></li>
<li id="li3"><img src="images/FT/2011GroupThumb.jpg" /></li>
<li id="li4"><img src="images/FT/JuniperSpringsSwimming.jpg" /></li>
<li id="li5"><img src="images/FT/HappyHourThumb.jpg" /></li>
<li id="li6"><img src="images/FT/HappyHourThumb.jpg" /></li>
</ul>
When I click each image I would like a larger version of the image to appear, sort of like this:
<li id="li2"><a href="images/FT/PineForestBig.jpg" target="_blank"><img src="images/FT/PineForest.jpg" /></a></li>
Unfortunately the CSS class associated with #Demo3 does a lot of manipulation to the class associated with all #li elements, so I have to add the anchor in programmatically.
I try doing it like this:
i = 1;
while ($("#li" + i).length > 0) { // If the li id exists
myLink = "images/FT/Page" + i + ".jpg"; // create a link
var element = document.getElementById("li" + i); // get the element
element.onclick = function () { // create an onclick for this element
var win = window.open(myLink, '_blank');
win.focus();
}
i++;
}
This does not work! For whatever reason, each image gets the same onclick as the last one in the list.
Does anyone know what I'm doing wrong or how to solve this so that each image contained within each li element gets its own onclick?
Any other suggestions?