3

Im trying to save some HTML in a JS variable by using the backtick formatting however is it possible to preserve the HTML variable according to the following example

var msg = "This is a test" + "\n" + "Test"

Im attempting to store this variable as a HTML paragraph while keeping the linebreaks

var emsg = '<p style="white-space: pre-wrap;"><\"{msg}"\</p>'

But when sending that content in an email to myself (Using Emailjs) I get the following

<"{msg}"

Any clue what I'm doing wrong? Thanks!

3 Answers 3

4
  1. You are using single quotes ('), not backticks (`)

  2. Placeholders in template literals are indicated by a dollar sign ($), which you are missing.

var msg = "This is a test" + "\n" + "Test"

var emsg = `<p style="white-space: pre-wrap;"><\"${msg}"\</p>`

console.log(emsg)

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

Comments

2

You could go with template literals like @spectric showed. or you can go with simple quote using + to seperate it with msg variable

var msg = "This is a test" + "\n" + "Test";//    V     V
var emsg = '<p style="white-space: pre-wrap;"><\"'+msg+'"\</p>';
console.log(emsg);

Comments

1

as described + removing the extra <\" and "\ probably

var emsg = <p style="white-space: pre-wrap;">${msg}</p>

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.