3

I try to send HTML but I get an error when writing the value at the content key as multiline string. What is the valid syntax for this?

var data = {    
        "subject":"Test",
        "content": "{<body><br><br><table style='text-align: left; border: 10px solid
                   #f5f5f5;padding:36px;margin:0px auto;background-color: #fff;'
                   class='maincontainer' cellspacing='0 cellpadding='0'>}"
      };

But it shows error.How to rectify?

1
  • Edited to restore original meaning. If a follow-up question arises, a new question should be asked. Otherwise, all previously given answers would get invalidated. The idea is to have something like a Wiki. So ask your questions in a way that other also can benefit from it. Commented Oct 14, 2020 at 6:22

5 Answers 5

6

ECMAScript 6 (ES6) introduces a new type of literal, namely template literals. They have many features, variable interpolation among others, but most importantly for this question, they can be multiline.

A template literal is delimited by backticks:

var html = `
  <div>
    <span>Some HTML here</span>
  </div>
`;

source: https://stackoverflow.com/a/805113/13741787

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

2 Comments

var html = ` <div> <span>Some HTML here</span> </div> ; In this i need to specify a variable var html = <div> <span>Fstname</span> </div> `; var fstname = "sabari";
@SabrilogeshK Please read carefully though Creating multiline strings in JavaScript. It explains how to interpolate variables into the template.
0

You can use string concatenation for that. Like this :

var data = {    
        "subject":"Test",
        "content":"{<body><br><br><table style='text-align: left; border: 10px solid" + 
                   " #f5f5f5;padding:36px;margin:0px auto;background-color: #fff;'" +
                   " class='maincontainer' cellspacing='0 cellpadding='0'>}"
      };

Comments

0
var data = {    
    "subject":"Test",
    "content":'<body><br><br><table style=\'text-align: left; border: 10px solid\n' +
              '#f5f5f5;padding:36px;margin:0px auto;background-color: #fff;\'\n' +
              'class=\'maincontainer\' cellspacing=\'0 cellpadding=\'0\'>'
  };

In this way, you can get the proper reesult.worked for me.

Comments

0

first of all your question is not clear. but I'm trying to understand it. you need string to be inserted inside html tag later. ECMAScript 6 (ES6) introduces a new type of literal, namely template literals. so change your code to.

var data = {    
        "subject":"Test",
        "content":`{<body><br><br><table style='text-align: left; border: 10px solid" + 
                   " #f5f5f5;padding:36px;margin:0px auto;background-color: #fff;'" +
                   " class='maincontainer' cellspacing='0 cellpadding='0'>}`
      };

now if you want to add variable inside you html string.. ex var test = 'hello world';

// variable inside your html string
var html = `<div class="new_class">${test}</div>`

Comments

-3

//  This is how we can create multiline strings in js
// 1. by using (\n) adding line space 
// 2. by using (+) concaginate two strings 

let string = "Hello , This is Pranav. Are you doing well ? \nAnd here i am creating a multiline string in js"
console.log(string)

let string2 = "Hello , This is Pranav. Are you doing well ?" + " And here i am creating a multiline string in js"
console.log(string2)

1 Comment

Concatenating strings does not add newlines or make them multi-line strings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.