-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
  • 340
  • 2
  • 14

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
  • 669,327
  • 51
  • 454
  • 560
2

The cleanest approach is to use template literals:

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