15

How do i declare a variable in jquery with multiple lines like,

original variable:

var h = '<label>Hello World, Welcome to the hotel</label><input type="button" value="Visit Hotel"><input type="button" value="Exit">';

the variable i want to declare:

var h = '<label>Hello World, Welcome to the hotel</label>
              <input type="button" value="Visit Hotel">
              <input type="button" value="Exit">';

3 Answers 3

27

You can use \ to indicate that the line has not finished yet.

var h= '<label>Hello World, Welcome to the hotel</label> \
              <input type="button" value="Visit Hotel"> \
              <input type="button" value="Exit">';

Note: When you use \, the whitespace in the following line will also be a part of the string, like this

console.log(h);

Output

<label>Hello World, Welcome to the hotel</label>               <input type="button" value="Visit Hotel">               <input type="button" value="Exit">

The best method is to use the one suggested by Mr.Alien in the comments section, concatenate the strings, like this

var h = '<label>Hello World, Welcome to the hotel</label>' +
              '<input type="button" value="Visit Hotel">' +
              '<input type="button" value="Exit">';

console.log(h);

Output

<label>Hello World, Welcome to the hotel</label><input type="button" value="Visit Hotel"><input type="button" value="Exit">
Sign up to request clarification or add additional context in comments.

5 Comments

+1 or you can just add \n if you want to put a enter in output like <label>Hello World, Welcome to the hotel</label>\n <input type="button" value="Visit Hotel">\n <input type="button" value="Exit">';
@imsiso This looks like HTML. Why would he want to append \n? :)
@thefourtheye - maybe because of improving HTML formatting and/or hide PHP extension and showing page as its a HTML? I just guessed that (-:.
@Mr.Alien Thanks yaar :) It was a very good suggestion, so I took it in.
That easy? LOL... thanks for the answer... I was doing some html append to a table which is "<tr>..alot of element..</tr>" and doing it in one line is really killing me.. I tried searching in the nets and just cant find it. Thanks a lot!! Here is my actual code.. var lowertr='<tr id="trLowerEdit_ID_'+staffid+'" class="trLower_cls">\ <td><label>Username</label></td>\ <td><input type="text" value="'+username+'"></td>\ </tr>';
10

Edit

Now you could also make a use of ES6 Template Literals.

let str = `
  some
  random
  string
`;

You can also interpolate the variables in the above string with an ease, without using concatenation, like:

let somestr = 'hello',
str = `
  ${somestr}
  world
`;

Old answer

@thefourtheye answer is perfect, but if you want, you can also use concatenation here because sometimes \ will be misleading, as you will think those are literal characters ..

var h = '<label>Hello World, Welcome to the hotel</label>';
    h += '<input type="button" value="Visit Hotel"> '; 
    h += '<input type="button" value="Exit">';

console.log(h);

Demo

Comments

3

In ES6 you can achieve this by simply declaring a variable as:

const sampleString  = `Sample
                       text
                       here`;

This in return evaluates to 'Sample \ntext\nhere' You can read all about ES6 rules of multiline strings from here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.