show small previews of pictures stored in a JavaScript array by using a for-loop

Currently stuck on a university project where I've set up an array of images like this:

var testdata = [
  {
      "imageID": 17,
      "uploadedDate": "2020-07-31 03:56:56.243139",
      "filepathOriginal": "test_images/image1.jpg"
  },
  {
      "imageID": 18,
      "uploadedDate": "2020-07-31 04:06:14.711057",
      "filepathOriginal": "test_images/image2.jpg"
  },
  {
      "imageID": 19,
      "uploadedDate": "2020-07-31 04:08:10.360168",
      "filepathOriginal": "test_images/image3.jpg"
  }
];

I'm looking to loop through this array and show each image in a modal for user viewing. I've been trying to use a for-loop without success. Does anyone have any suggestions or solutions?

The HTML code for the modal is as follows:

<!--modal header: includes functional closing button for modal to return to page-->
<div class="modal-header">
  <span class="closeBtn">&times;</span>
  <h2>Image search and processing</h2>
  <p>Please select the dates you wish to search: </p>
</div>
<div class="modal-body"> 
  <form id="modal-form" class="form" >

    <!--date picker for querying API-->
    <input id="datepicker"/>
    <button id="submit">Submit</button>
    <script type="text/javascript" src="js/pipeline.js"></script>

    <!--floating buttons to submit process or clear form/array and return to page-->
    <button type="submit" class="floating-btn" value ="submit">+</button>
    <button type="reset" class="floating-btn2" value ="reset" onclick="return hideImage()">x</button>

    <!--hidden input that adds selected images to an array for processing-->
    <input type="hidden" name="list" id="list">

  </form>
</div>

Your help on this matter would be immensely valuable!

Answer №1

When approaching this problem, there are various methods to consider. One option is to use a for loop, but in the example provided, the forEach method is employed as it requires less code and functions similarly. By utilizing a forEach loop, each element in the array is iterated through sequentially.

The function outlined aims to access the "testdata" array, iterate through each data item, fetch the element "#demo", and update the HTML content to display a paragraph tag containing the imageID from the object. This demonstration can be modified to output an image tag using the filepathOriginal attribute from the object instead. The "+=" operator signifies that after each call, the new paragraph tag should be appended rather than replacing the existing one.

var testdata = [
  {
      "imageID": 17,
      "uploadedDate": "2020-07-31 03:56:56.243139",
      "filepathOriginal": "test_images/image1.jpg"
  },
  {
      "imageID": 18,
      "uploadedDate": "2020-07-31 04:06:14.711057",
      "filepathOriginal": "test_images/image2.jpg"
  },
  {
      "imageID": 19,
      "uploadedDate": "2020-07-31 04:08:10.360168",
      "filepathOriginal": "test_images/image3.jpg"
  }
];

testdata.forEach(data => {
  document.getElementById("demo").innerHTML += '<p>' + data.imageID + '</p>';
})
<div id="demo"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting a timestamp from PHP in JSON format to date and time using JavaScript

Within the JSON file, there is a timestamp associated with each user login. An example of this timestamp is: timestamp: "1541404800" What steps should be taken to convert this timestamp into date and time format? ...

Tips on incorporating a button to enable text formatting to 'bold' within a Text Area on an HTML webpage:

In the process of creating an online text editor, I am looking to incorporate a button above the text area that can be used to make selected text and/or upcoming text bold. To provide more clarity, I envision adding a bold button similar to the one found a ...

What is the best way to add color to a div at the bottom of a page using

https://i.sstatic.net/5kOt7.png I noticed in the image that there is a navy blue color applied to a div at an angle. How can I achieve this effect using Bootstrap in my ASP.NET Core project? ...

Is there a way to listen for router config Data subscription upon NavigationEnd event?

At the moment: I'm using router.events to fetch information from {path: '', component: HomeComponent, data:{header:true}}, router configuration ngOnInit() { this.router.events.filter(e => e instanceof NavigationEnd).subscribe(event ...

React: Perform edits and deletions through a convenient pop-up menu for each item in a list

In my project, I have a list container component that acts as the parent and maps out the list rows. Each list row component, which is the child, contains an item with buttons to toggle a pop-up menu. This menu has options for editing and deleting the item ...

How can I redirect after the back button is pressed?

I am currently working with a trio of apps and scripts. The first app allows the user to choose a value, which is then passed on to the second script. This script fetches data from a database and generates XML, which is subsequently posted to an Eclipse/J ...

Creating interactive form fields using React

How can I update nested fields in react forms? First, create a new item using handleAddShareholder. Next, delete an existing item with handleRemoveShareholder. To change details of an item, use handleShareholderNameChange. You can then add a new array ...

Executing PHP Functions with Script Tags

I am trying to use PHP to output the <script></script> tag. Here is the code I am using: <?php echo "test"; echo "<br>"; echo '<script src="http://mywwebiste./mycode.js" type="text/javascript& ...

Utilizing Blurred Images in the Header

I want to create a blurred background image with text and buttons on top of it, but the blur effect is also affecting the text and buttons. I'm not sure how to keep them separate. header { background: url("street-238458.jpg") no-repeat center; ...

Challenges encountered with input outcomes

I am facing an issue with input results. I have a button that triggers a function to check for empty input fields. However, when I click the button, it always falls into the last if statement and displays as if the fields are not empty. I have already att ...

Encountering a 404 error while trying to use the autolinker

I have been struggling with this issue for the past day and can't seem to solve it on my own. Essentially, I am attempting to use Autolinker.js to automatically convert URLs into clickable hyperlinks in my chat application. However, every time I try ...

Difficulty encountered when deploying cloud function related to processing a stripe payment intent

I've been troubleshooting this code and trying to deploy it on Firebase, but I keep running into a CORS policy error: "Access to fetch at ... from origin ... has been blocked by CORS policy." Despite following Google's documentation on addressin ...

Clicking on the modal will trigger its closure

Is there a way to prevent Bootstrap 4 modal from closing when the user tries to leave the page but clicks on it instead? I have attempted various solutions from this platform, but none have worked for me. I am running out of ideas. This snippet shows my ...

Tips for integrating and utilizing the MSAL (Microsoft Authentication Library for JavaScript) effectively in a TypeScript-based React single page application

Issue I'm encountering difficulties importing the MSAL library into my TypeScript code. I am using the MSAL for JS library with typings in a basic TypeScript/React project created using create-react-app with react-typescript scripts. As someone who i ...

I am having issues with my Django application where I am unable to check

I have an HTML page where I want to only display the search bar if the table object passed in is not empty. However, my check doesn't seem to be working correctly. Here's my code: <!-- We'll show the search bar only if the user has acces ...

The Angular.js resource REST request encountered a TypeError due to a function being undefined

My goal is to update data using a REST call. I have already made a GET request to populate the form, and upon clicking the update button, I intend to initiate a PUT call. However, instead of successfully executing the PUT call, I encounter a Type Error. B ...

When using Firefox to scale down a div with CSS `moz-transform`, the border details are unfortunately not retained

When viewing the following HTML in Firefox, you may notice that the right and bottom border is missing. However, Chrome and Internet Explorer display it correctly. Is this a bug in Firefox, or is there another method I can use to make it look consistent ...

Removing a Button from a Form Using JavaScript

Here is a snippet of my HTML code: <form id="form"> <input id="deleteNumber" name="del" type="hidden" /> <input id="addAddress" name="addAddress" type="hidden" /> ... ... ... <a href="javascript:deleteAddress();" class="deleteItem" ...

Utilizing Spaces in Parameters for discord.js

My current challenge involves sending tweets from discord.js. However, I encountered an issue where messages with spaces are being posted as "hello,from,discord" instead of "hello from discord". Can anyone help me rectify this? const Twitter = require(&apo ...

How can you disable an 'option' HTML5 element using the AngularJS methodology?

How can I disable an 'option' element in HTML5 using AngularJS? Currently, I am working with AngularJS v1.2.25. Here is the Plunk Link for reference: https://plnkr.co/edit/fS1uKZ <!-- CODE BEGIN --> <!DOCTYPE html> <html> ...