2

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
  • 16,715
  • 5
  • 52
  • 86
Sabrilogesh K
  • 197
  • 4
  • 16
  • 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

5 Answers5

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

Sarkar
  • 1,225
  • 7
  • 21
  • var html = `
    Some HTML here
    `; In this i need to specify a variable var html = `
    Fstname
    `; var fstname = "sabari";
    – Sabrilogesh K Oct 14 '20 at 06:05
  • @SabrilogeshK Please read carefully though [Creating multiline strings in JavaScript](https://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript). It explains how to interpolate variables into the template. – ggorlen Apr 29 '23 at 21:38
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,604
  • 1
  • 5
  • 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
  • 456
  • 1
  • 3
  • 9
-2

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