0

i would like to show to user Text with variable inside, is that possible somehow since im injecting innerHTML?

  const price = document.querySelector("div.basket div.price em").innerText
  const nettoprice = price * 0.77
  div.className = "alert";
  div.innerHTML = "<div class="netto-print">
    Netto Price: $nettoprice
  </div>";

  document.body.append(div);

2 Answers 2

2

Yes, you can use template literals:

  div.innerHTML = `<div class="netto-print">
    Netto Price: ${nettoprice}
  </div>`;

For more information about how to handle texts check Handling text by MDM.

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

Comments

0

You can solve this with backtick strings:

function escapeHTML(str){
    return new Option(str).innerHTML;
}

div.innerHTML = `<div>${escapeHTML(price)}</div>`

Comments

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.