Home > Software design >  Storing HTML in a JS variable but preserving the HTML variables
Storing HTML in a JS variable but preserving the HTML variables

Time:09-25

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!

CodePudding user response:

  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)

CodePudding user response:

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);

CodePudding user response:

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

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

  • Related