Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 3014
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 1, 20222022-06-01T23:20:44+00:00 2022-06-01T23:20:44+00:00

json – Why the list of Modal which is generated from fetch only show the last item?

  • 0

[ad_1]

I’m creating a list of modal through fetching local json file, but when I clicked the learn more button, all cards had the information of the last item.

const cards = document.querySelector(".cards");

async function getData() {
  const response = await fetch("./modals/modal.json");
  const data = await response.json();
  console.log(data);
  data
    .map((item) => {
      cards.innerHTML += `
           <div class="card slide">
            <img
              src="${item.image}"
              alt="${item.name}"
              class="card--img"
            />
            <h3>${item.name}</h3>
            <button class="card--btn-light open-modal">Learn more</button>
            </div>
            <div class="modal">
            <div class="modal-content">
                <div class="modal--img">
                  <img src="${item.image}" alt="${item.name}" />
                </div>
                <div class="modal-desc">
                  <h3>${item.name}</h3>
                  <h4>${item.type}</h4>
                  <p>
                    ${item.desc}
                  </p>
                  <p><strong>Age: </strong> ${item.age} months</p>
                  <p><strong>Inoculations: </strong>none</p>
                  <p><strong>Diseases: </strong> none</p>
                  <p><strong>Parasites: </strong> none</p>
                  </div>
                  <div class="close-modal">✕</div>
              </div>
            </div>
         
    `;
    })
    .join("");

  // ---------------modal
  // works but only show last item
  const modal = cards.querySelectorAll(".modal");

  console.log(modal);
  modal.forEach((item) => {

    
    cards.addEventListener("click", (e) => {
      if (e.target.classList.contains("open-modal"))
        item.style.display = "flex";

      if (e.target.classList.contains("close-modal")) {
        item.style.display = "none";
      }

      window.addEventListener("click", (e) => {
        if (e.target === modal) {
          item.style.display = "none";
        }
      });
    });
  });

I am wondering how to show the right information of the clicked card. I have seen the solution of the modal issue but the content was generated from HTML, it’s a little different from this which the content was generated from fetching.

[ad_2]

  • 0 0 Answers
  • 5 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 3 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 5 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 4 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.