I am trying to understand this javascript-code I got that I’m supposed to use for a image gallery.
I’ve tried searching but I didn’t find anything that was useful for me. I am fairly new to the javascript language.
I like to get help with understanding how exactly I get this code to show the pictures in the HTML document. What do I write in the html?
var imgArray = ["pic1.jpg" , "pic2.jpg" , "pic3.jpg", "pic4.jpg"];
var galleryString = "";
for (var i = 0; i < imgArray.length; i++) {
GalleryString += "<img src=\" " + imgArray[i] + "\" />";
}
document.getElementById("galleryDiv").innerHTML = galleryString;
You just have a typo:
GalleryStringshould begalleryString. JavaScript identifiers are case-sensitive, soGalleryStringandgalleryStringare different variables. Once you fix that, you should see all images fromimgArrayinside yourgalleryDiv.