1

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

10
  • 2
    Start here Commented Nov 21, 2021 at 13:45
  • This is only really possible using template languages which compile to JS/HTML. Like Mustache, handlebars, JSX, etc. Commented Nov 21, 2021 at 13:50
  • could you elaborate @evolutionxbox I want to know Commented Nov 21, 2021 at 13:52
  • Tools like the ones I've already mentioned usually introduce an extra build step before producing the final HTML/JS. This is if you don't want server-side rendered code, like PHP, node, C#, etc. People use libraries like react if they need client-side reactive template code. Commented Nov 21, 2021 at 13:53
  • Can you share tilemap object Commented Nov 21, 2021 at 13:58

2 Answers 2

1

Like so?

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

You need something more than HTML to create dynamic content.

Sign up to request clarification or add additional context in comments.

2 Comments

a way to do it in the HTML only? if this is the only way im fine its just personal preference here
You can't handle variables in HTML in the way I think you're asking for.
0

You could do it this way:

let score;
// do score stuff
document.querySelector(".scoreboard").innertText = `Your score is ${score}`;

1 Comment

I know that but I want to do it in cleaner way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.