1

I have an array of objects stored in localStorage that dynamically makes articles and places them in a grid. The articles have an tag that has a unique data-target attribute. What I need is to access that data-target value on the page it links to.

On the other page I have this function to use information from the same array in localStorage. Each object has a "caseNr" property to work as a unique ID. What I need is an if-statement to check if caseNr is the same as the data-target value from the clicked tag on the other page. This is so that it shows information from the same article. In the code below "nr" is just to show how I think the if-statement should work, and I think it should be a variable holding the data-target from the clicked tag.

Thanks in advance!

Mabero
  • 57
  • 1
  • 7

1 Answers1

1

I'd suggest passing the value through the query string in the href

let button = `<a href="details.html?target=${i}" id="button${+i}">SE DETALJER</a>`;

Then on the resulting page (details.html) you could access it with window.location.search

  • Note that window.location.search will return something like "?target=42", so you'll still need to parse out the actual value. Depending on what browsers you need to support you could get some help with this by using URLSearchParams as shown in this answer
Josh G
  • 644
  • 7
  • 21