0

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?

The Fool
  • 10,692
  • 4
  • 27
  • 50
Sabrilogesh K
  • 157
  • 2
  • 12
  • 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. – The Fool Oct 14 '20 at 06:22

4 Answers4

4

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

Sarkar
  • 490
  • 3
  • 13
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'>}"
      };
Pranav Rustagi
  • 2,564
  • 1
  • 3
  • 18
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.

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>`
ash
  • 324
  • 1
  • 2
  • 8