Skip to content
Advertisement

Using a for loop to display HTML element using JavaScript

I’m currently working on a project where I have a gallery (Gallery.php) containing a carousel of images, if an image is clicked on, the ImageID (I have set) and the Image URL are added to a 2D array in the localStorage. I am attempting to retrieve the array from localStorage in the homepage (index.php) which works as confirmed by logging to the console, however I am struggling to find a way to loop through the array and display a bootstrap card where the header is the ImageID and the body is an image with the URL. I realise this would be easier in PHP, but the project briefing asks for this to be done explicitly in JavaScript. Here is the psuedocode for what I am trying to achieve

JavaScript

This is the function which sets the ImageID and URL, and then pushes to the localStorage array

JavaScript

I am trying to achieve a similar effect of using a foreach loop in PHP, I am also using JQuery in this project if a solution is available through it.

The PHP for this solution would be something along the lines of this if it helps you understand what I am trying to achieve.

JavaScript

Any help would be greatly appreciated!

Advertisement

Answer

Loop through the images, and for each element, create a card and append it to the destination (assume to be #target):

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement