-3

I have two questions:

  1. how do I pass the parameter in this href? id is a previously declared variable with value. I tried as written below but the id is not read
  2. Also, how can I read this id that I pass in the new html page (detailsbook)?

document.getElementById("book").innerHTML = "<a href='detailsbook.html?id=+id'>"+ myArray.Item.title + "</a>";

Thank you all.

a_l_e_x
  • 408
  • 1
  • 6
  • 20

2 Answers2

2

The easiest way to substitute variables into a string is with template literals rather than concatenation.

document.getElementById("book").innerHTML = `<a href='detailsbook.html?id=${id}>${myArray.Item.title}</a>`;
Barmar
  • 741,623
  • 53
  • 500
  • 612
2

The cleanest approach is to use template literals:

document.getElementById("book").innerHTML = 
  `<a href="detailsbook.html?id=${id}">${myArray.Item.title}</a>`;
szaman
  • 2,159
  • 1
  • 14
  • 30