The function is unable to load all items from the JavaScript data within the CSS modal

Having an issue with my js script. The function portfolioThumb is working correctly as it shows all thumbs, but the function portfolioModal only loads the first element from portfolioData. My modal uses a simple CSS with the :target pseudo-class. Everything was fine when I didn't have the modal.

I created a file called "portfolio.js" with the following content:

const portfolioData  = [
        {
        title: "Example1",
        desc: "Lorem ipsum dolor et miset execut lorem dausum dolor hamburgefonsz odnit",
        tech: "Axure, Photoshop, Illustrator ",
        photo: "images/example.png",
        thumb: "images/thumb1.png"
    },
    {
        title: "Example2",
        desc: "Lorem ipsum dolor et miset execut lorem dausum dolor hamburgefonsz odnit",
        tech: "HTML5, Sass, CSS3, jQuery, Javascript",
        photo: "images/example.png",
        thumb: "images/thumb1.png"
    },
    {
        title: "Example3",
        desc: "Lorem ipsum dolor et miset execut lorem dausum dolor hamburgefonsz odnit",
        tech: "Adobe After Effects, Adobe Premiere",
        photo: "images/example.png",
        thumb: "images/thumb1.png"
    }
    ];

      function portfolioThumb(portfolio) {
        return `
        <div class="project">
            <a class="btn" href="#open-modal"><img class="project-thumb" src="${portfolio.thumb}"></a>
        </div>`;
      }

      document.getElementById("portfolio").innerHTML = `
      ${portfolioData.map(portfolioThumb).join("")}`;

      function portfolioModal(modal) {
        return `
        <div>
            <a href="#projects" title="Close" class="modal-close">Close</a>
            <h1 class="project-title">${modal.title}</h1>
            <div class="project-desc">${modal.desc}</div>
            <img class="project-photo" src="${modal.photo}">
            <div class="project-desc">${modal.tech}</div>
        </div>`;
        }

        document.getElementById("open-modal").innerHTML = `
        ${portfolioData.map(portfolioModal).join("")}`;

and part of index.html

 <!-- PROJECTS -->
    <section id="projects">
        <div class="content">
          <div id="portfolio"></div>
          <div id="open-modal" class="modal-window"></div>
        </div>
    </section> 

Answer №1

Woohoo! Success is mine! When dealing with multiple modals, it's crucial to keep track of numbers. By assigning id numbers to arrays in the constant portfolioData, as well as to links like

a href="open-modal-${portfolio.id}
, everything falls seamlessly into place!

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

I am struggling to format my JSON file to look "pretty" with iPython

Implementing from IPython.display import JSON is my way of structuring json. Yet, the output I receive is: <IPython.core.display.JSON object> This is how I wrote my code: from IPython.display import JSON with open("world_bank_projects.json" ...

Clicking on a link with an onclick method renders selenium ineffective in producing any action

The specified element is: <a href="#" onclick="submitViaAjax(null, url);return false;">Add</a> After using element.click() to try clicking the link, the onclick method fails to execute. Is there a way to ensure the method is executed success ...

When the cursor hovers, a drop-down menu appears simultaneously

Currently utilizing the Bigcommerce stencil theme, I have encountered an issue where both dropdown menus appear when I hover over a link. My goal is to have only one dropdown menu pop up when hovering over a specific link. https://i.sstatic.net/paVoY.png ...

Error encountered while using $.each: Unable to utilize 'in' operator to search for '37' within the context

My JSON Object structure is as follows : var jsonData = "{'key1' : 'value1', 'key2' : 'value2'}"; Since it's already in JSON format, parsing isn't necessary. I simply want to loop through the object using ...

Troubleshooting my code: The mystery of why Google Map directions won't cooperate!

Can anyone help me figure out why my code for drawing a path between multiple points on a map isn't working? I've tried the code below, but it doesn't draw any paths. What could be causing this issue and how can I solve it? var myTrip = [] ...

AngularJS anticipates the completion of a lone asynchronous request

Attempting to assign data to the scope after an asynchronous call. There is an array named companies in the factory. factory.getByCategoryId = function (id) { $http.get('http://localhost/campaign?access-token=12345&id=2').then( func ...

Shift a div along the x-axis, followed by the y-axis movement

Provided below is an example that showcases my script: http://jsfiddle.net/jewb/tM5h7/4/ The script I have written looks like this: var minHeight = $(window).scrollTop(); var maxHeight = $(window).height(); var middleHeight = (maxHeight + minHeight) / 2; ...

Error: Vuex commit fails due to JSON circular structure issue

Using Vue.js along with the vuex store, I make an API call to validate an item, which returns arrays of errors and warnings. Below is my vuex action : export function validateitemReview ({ commit, dispatch, state }, { reviewId, type, itemreviewData }) { ...

Preserve data on page even after refreshing in Angular

Upon opening my website, I expect to see "Finance/voucher" at the top. However, after refreshing the page, only "Finance" appears which is not what I want. I need it to always display "Finance/voucher" even after a page refresh. I have included all the r ...

Switch the paper tab to a dropdown menu in Polymer.js

Can someone assist me in transforming the paper tab into a paper drop down menu in polymer JS? I want the drop-down to appear with a list of values when hovering over the Top menu. Activity Execution <paper-tab cla ...

Troubleshoot the issue of ng-click not functioning properly when used in conjunction with ng-repeat with direct expressions

Having some trouble with an ng-repeat block and setting a $scope variable to true with an ng-click expression. It doesn't seem to be working as expected. Can anyone help out? Check the plnkr for more information. HTML: selected: {{selected}} < ...

How can I effectively assign model data to a service property in an AngularJS controller?

I have established a service to facilitate the sharing of data/state across multiple controllers. One of the controllers updates certain properties within the service using scope data through a save function. The updated data is then accessed by other cont ...

Successive jquery functions are executed independently upon being called

I've encountered a simple problem that has stumped me despite trying various approaches to consolidate these functions in one HTML file. The issue arises when the first function is invoked, causing the second function to execute simultaneously and lea ...

Check if any element within a class satisfies the specified condition using Jquery

I am looking to determine if any element within a specific class meets a set of conditions. For example: $(document).on('click','.modalInner_form_nav', function(){ var input = $(this).parents('.modalInner_form').find(' ...

Is it possible to use PHP to replace a specific string on a webpage with a unique identifier from a select box?

I am currently attempting to replace a string with a select box. Below are the codes that illustrate my objective. The following code is the original version that I want to modify. <div id="a"> <div id="a1">{box}</div> <div id=" ...

The Sizzle.js error, "Uncaught TypeError: undefined (reading 'expr')," is causing some trouble

$.expr[':'].containsCaseInsensitive = function (n, i, m) { return jQuery(n).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0; }; .expr is not recognized. To ensure it's defined, I included a CDN link below: <script src=&qu ...

Can anyone share tips on implementing animations during transitions when using $location.path for redirection in AngularJS?

In my project setup, I am utilizing AngularJS for the structure and backend functionalities, while JqueryM is being used for the UI design elements. Currently, I'm redirecting using $location.path with ng-click. Is there a method to incorporate JQM d ...

Encounter TypeScript error when using getFirebaseAdmin from next-firebase-auth

I am currently utilizing a package called next-firebase-auth. I am encountering an issue when calling getFirebaseAdmin from next-firebase-auth and attempting to use it, specifically when using db as a reference in doc(db). The error message received is: ...

Using Angularjs to bind an array element within an ng-repeat inside a directive

Having trouble displaying the elements of an array using ng-repeat with a directive. The binding is not working correctly and showing empty values. To view the code, visit http://jsfiddle.net/qrdk9sp5/ Here is the HTML: <div ng-app="app" ng-controlle ...

Saving the data of a variable to a text file

I've developed a simple system to capture a person's First and Last name (this is the basic code, of course). But I'm interested in outputting the value of a variable to a text file. I've tried using some php, but it doesn't seem t ...