Home > other >  How would I insert a JavaScript variable into HTML?
How would I insert a JavaScript variable into HTML?

Time:11-22

I want to do something like this:

<span class="scoreboard">Your score is ${score}</span>

and then have javascript like this:

let score;
// do score stuff

Basically have dynamic HTML. I googled this but didn't find anything relevant. I have this right now

function draw() {
  // ignore this game.screen.innerHTML = "";
  // and this tilemap.map.forEach(elem => game.screen.innerHTML  = `${elem}<br>`);
  game.scoreboard.innerHTML = `Score: ${player.collected}`;
}

this is mostly personal preference so I understand that its irrelevant

CodePudding user response:

Like so?

let score = 5;
document.querySelector('span.scoreboard').innerText = `Your score is ${score}`;

You need something more than HTML to create dynamic content.

  • Related