0

I have a javascript function displayImages() to display all the images in a directory. I am calling this function inside a DIV "photo_gallery" so that the images display in that DIV only (after page loads)

However I also needs to display the image gallery dynamically and for which I have to call this function from outside DIV i:e from other function inside my js file.

<div id="photo_gallery" style="display:none">
  <ul>
    <script>displayImages();</script>
</ul>

</div>

Can someone please advise

Below is my function :

Note : (here images is an array of images)

function displayImages() {

 for (i=0;i<images.length;i++) {

  document.write("<a href='#pop1'>");

   document.write("<img onclick='currentimage(&quot;" + images[i] + "&quot;)'    class='photo'  src='" + images[i] + "' width='160' height='120'/>");
 document.write("</a>");


}
}

1 Answer 1

1

You have to execute the javascript-function in the body onload event or in a script tag outside your div-container.

<body onLoad="displayImages()">

In your function displayImages() you have to set the innerHTML (content) of the Div-Container f.ex.:

yourHtmlGalleryGeneratedContent = '';
for (i=0;i<images.length;i++) {
 yourHtmlGalleryGeneratedContent += "<a href='#pop1'>";
 [...]
}
document.getElementById('photo_gallery').innerHTML = yourHtmlGalleryGeneratedContent;
Sign up to request clarification or add additional context in comments.

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.