Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatting jQuery strings that should span multiple lines

One nice thing with jQuery is that the syntax (such as when chaining) allows your script to span multiple line breaks for ease of formatting and readability.

Is there an equivalent/preferred method when one is adding a large string of HTML?

For instance, it'd be nice to do something like this:

 $("#theObject")
    .doSomething()
    .append("
        <div>
             <div>
                  Hello World
             </div>
        </div>
    ")

That doesn't work but is there something akin to that to make it easier to read HTML being formatted within a jQuery script?

like image 958
DA. Avatar asked Nov 12 '09 19:11

DA.


People also ask

What would you use to enclose multi line strings in JavaScript?

There are three ways to create strings that span multiple lines: By using template literals. By using the + operator – the JavaScript concatenation operator. By using the \ operator – the JavaScript backslash operator and escape character.

How do you write multi line strings in template literals in JavaScript?

There are three ways to create a multiline string in JavaScript. We can use the concatenation operator, a new line character (\n), and template literals. Template literals were introduced in ES6. They also let you add the contents of a variable into a string.

What is multiline in JavaScript?

This is a long message that spans across multiple lines in the code. In the above example, the template literal ` ` is used to write multiline strings. The template literal was introduced in the newer version of JavaScript (ES6). Some browsers may not support the use of template literals.

Can we write multiple line in JS?

You can have multiline strings in pure JavaScript. This method is based on the serialization of functions, which is defined to be implementation-dependent.


3 Answers

Add a \ at the end of each line

$("#theObject").doSomething()
.append("
    <div>\
         <div>\
              Hello World\
         </div>\
    </div>\
")

Edit 2016:

If you're using ES6 (ES2015), you now can use template literals.

like image 73
Geuis Avatar answered Oct 19 '22 15:10

Geuis


 $("#theObject")
.doSomething()
.append(
    "<div>"
         +"<div>"
              +"Hello World"
         +"</div>"
    +"</div>"
);

It'd be best practice to load your .append() content as a var, like so:

var append_me =
     "<div>"
         +"<div>"
              +"Hello World"
         +"</div>"
    +"</div>";

$("#theObject").doSomething().append(append_me);
like image 36
iamkoa Avatar answered Oct 19 '22 15:10

iamkoa


This can be done easier!

Don't use the "+" or the "/".

Use template literals: `
enter image description here

$("#theObject").doSomething()
.append(`
    <div>
         <div>
              Hello World
         </div>
    </div>
`)

Documentation on template literals can be found here.


As @Geuis commented, this can only be used from es2015/ES6 (Can I use template literals).

like image 7
Brampage Avatar answered Oct 19 '22 14:10

Brampage