I'm trying to build simple javascript/jquery gallery. I suppose this is a pretty specific question.
The logic is as follows: to make as many divs as array contains elements(var sum). These elements present image names. I realized that in first for loop(first append function adds image and the second one adds name list - to navigate between images). In second for loop I set initial value (show me first image) and it works.
I met with an obstacle when trying to assign function to clicked element from name list. Third for loop obviously works(since it's copy of the second one). Is there any syntax error?
Code:
<div id="container"> //contains images
</div>
<div id="list"> //contains navigation numbers
</div>
<script>
var array = ['0', '1', '2']; //img names
var sum = array.length;
var currentId = 'a' + array[1];
$(document).ready(function () {
for (var i = 0; i < sum; i++) {
$("#container").append("<img class='a' src='img/" + array[i] + ".jpg' id='a" + array[i] + "'>");
$("#list").append("<a href='#a" + array[i] + "' onclick='checkButton()'>" + array[i] + "</a><br />");
}
for (var i = 0; i < sum; i++) {
var inactiveId = 'a' + array[i];
if (currentId !== inactiveId) {
document.getElementById(inactiveId).style.display = "none";
}
}
currentId1 = this.id;
$(currentId1).click(function () {
document.getElementById(currentId1).style.display = "block";
for (var i = 0; i < sum; i++) {
var inactiveId = 'a' + [i];
if (inactiveId !== currentId1) {
document.getElementById(inactiveId).style.display = "none";
}
}
})
});
</script>