0

I am building an app using HTML, CSS and vanilla JS to help learn my way around the languages and I have been stuck on a problem for a while.

Basically I have an index.html page that populates with football fixtures when a button is clicked. This works fine.

Then, when a certain fixture button is clicked, I want to pull specific data for that specific fixture and display it on a different page when loaded. The button click loads a new html page, but then should populate that page with the relevant fixture data.

Through lots of trial and error, only part of the function works. There is quite a lot of code from between HTML and JS so I don't want to share too much to make this more complex, as I am sure I am probably missing something simple.

But, here is the code that picks up the unique fixture id from the fixture card that is clicked on the index.html page.

  const matchButton = document.querySelectorAll(".match-button");
  for (i = 0; i < matchButton.length; i++) {
    matchButton[i].addEventListener("click", (e) => {
      let matchID = localStorage.setItem("matchID", e.target.id);
      stats
        .getFixture(e.target.id)
        .then((fixtureData) => {
          updateQuizMatchData(fixtureData);
        })
        .catch((err) => {
          console.log(err);
        });
      document.location.href = "/quiz.html";
    });
  }
};

Originally, I did not store anything in localStorage, I just tried to call the function that makes an API call from the id directly. But the function doesn't seem to fire. it seems to break at setting the localStorage part. Is this because of the page load to a new page?

I stored the id in localStorage thinking that I could pull from that via another js file attached to the quiz.html page. As you can probably see, I am overcomplicating things maybe?

Anyhow, this is code above is meant to load the quiz.html page and then display the specific fixture related data from the fixture button that is clicked.

The code fires and updates localStorage and loads the quiz.html page. So I know it works to that point. But the other code does not fire. The other code that is meant to fire is an API call, followed then by this to update the quiz.html page dynamically...(not sure if this code is relevant to the issue I am seeing, as the API function call is not being fired either, which is why this below may not fire)

const updateQuizMatchData = (fixtureData) => {
  console.log(fixtureData);
  let date = "11-11-2001";
  console.log(date);
  teams.innerHTML = `
    <div class="home-team m-4 pl-6">
    <img
      class="team-badge w-40 h-40"
      src="${fixtureData.teams.home.logo}"
      alt="home-team"
    />
    <p
      class="home-score text-center text-6xl m-6 bg-red-500 rounded-lg text-white p-6 shadow-xl"
    >
      ${fixtureData.goals.home}
    </p>
  </div>
  <div class="vs-text pt-20">
    <p class="text-center text-6xl font-extrabold p-4">VS</p>
    <p class="location text-center">${fixtureData.fixture.venue.name}</p>
    <p class="date text-center">${date}</p>
  </div>
  <!-- Away team detail section -->
  <div class="away-team m-4 pr-6">
    <img
      class="team-badge w-40 h-40"
      src="${fixtureData.teams.away.logo}"
      alt="away-team"
    />
    <p
      class="away-score text-center text-6xl m-6 bg-lime-500 rounded-lg text-white p-6 shadow-xl"
    >
    ${fixtureData.goals.away}
    </p>
  </div>
    `;
};

I know this doesnt fire because the console does not log the data at the top. All that happens is my quiz.html page loads my dummy content.

I have been battling this for a while and getting frustrated. What am I missing? Or not doing correctly?

I have tried moving code onto a separate js file but this has no impact. I also have attempted to fire the above function via window.load, but this doesn't fire it either. The introduction of localStorage was also to see if I could just fire a function from page load using localStorage ID that I set from button click as the parameter in the API call I make to get the specific fixture data. I hope this makes sense.

What am I missing?

How can I update the quiz.html from a specific fixture button that is clicked?

OrcaDrive
  • 17
  • 5
  • 5
    When you trigger an API call on one page and immediately navigate to a new page, the initial call will never resolve. You should trigger the call and handle the response inside the quiz.html page. Rather than storing the match ID in local storage, I'd suggest adding it to the URLs query string. E.g. `document.location.href = ".quiz.html?matchId=" + e.target.id;`. In quiz.html, you can retrieve the ID from the URL by doing `const matchId = new URLSearchParams(window.location.search).get("matchId");` – user3297291 Nov 15 '22 at 14:41
  • @user3297291 - Thanks for this, and it does make sense. Could I ask one other question. I have implemented the above and it works fine. But, how am I then best triggering the API function and the function that updates the page content above, as the page quiz.html page actually loads? Is this best handled on a separate JS file for the quiz.html page only, or can this be handled on the same JS file as all other events and function calls that I am calling on the index.html page? – OrcaDrive Nov 16 '22 at 11:02
  • I'm afraid I don't really have an answer other than: "it depends..." These types of questions are often [hard to discuss on stack overflow](https://stackoverflow.com/questions/8410298/one-js-file-for-multiple-pages). I think it's best to just try and make something work and ask a follow-up question if you run in to concrete issues. – user3297291 Nov 16 '22 at 11:55
  • Yes totally makes sense. Let's see what I can figure out. Thanks again for the initial help as that has solved a large part of this problem for me. – OrcaDrive Nov 16 '22 at 13:53

0 Answers0