Sabrilogesh K
Sabrilogesh K

Reputation: 307

How to create multiline string in js

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?

Upvotes: 3

Views: 7671

Answers (5)

Pranav Malwad
Pranav Malwad

Reputation: 1

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

Upvotes: -2

ash
ash

Reputation: 516

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>`

Upvotes: 0

Hassan Ahmed
Hassan Ahmed

Reputation: 13

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.

Upvotes: 0

Sarkar
Sarkar

Reputation: 1879

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

Upvotes: 6

Pranav Rustagi
Pranav Rustagi

Reputation: 2731

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'>}"
      };

Upvotes: 0

Related Questions