-1

php - this works:

$html = "
    <div class='part partb4'>
    <img class='imgb4' src='imgdef/01.jpg' alt='img'>
    </div>";
echo $html;

javascript - doesn't work:

let html = "    
    <div class='part partb4'>
    <img class='imgb4' src='imgdef/01.jpg' alt='img'>
    </div>";
    $(html).insertBefore($('.part').eq(0));

javascript - this works:

let html = "<div class='part partb4'><img class='imgb4' src='imgdef/01.jpg' alt='img'></div>";

$(html).insertBefore($('.part').eq(0));

How to make the second example valid? I'm on windows, Notepad++.

Thanks.

qadenza
  • 9,025
  • 18
  • 73
  • 126

5 Answers5

1

For the multiline you should use a single backticks ` (shift + button before 1)

let html = `
  <div class='part partb4'>
<img class='imgb4' src='imgdef/01.jpg' alt='img'>
</div>
`;
Raheel Khan
  • 464
  • 10
  • 12
1

by adding a backslash ( \ ) as the last character in the line, you can't even have a space bar behind it, because then it won't cancel the line break.

let html = "    
<div class='part partb4'>\
<img class='imgb4' src='imgdef/01.jpg' alt='img'>\
</div>";
$(html).insertBefore($('.part').eq(0));
Ekene Madunagu
  • 323
  • 1
  • 3
  • 7
0

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 class='part partb4'>
<img class='imgb4' src='imgdef/01.jpg' alt='img'>
</div>
`;
0

Javascript allows strings to be split in ES6 using the backticks:

let html = `    
<div class='part partb4'>
<img class='imgb4' src='imgdef/01.jpg' alt='img'>
</div>`;
$(html).insertBefore($('.part').eq(0));
Nathan Champion
  • 1,291
  • 1
  • 14
  • 23
0

You are using multiline string thats why its not working I guess use template literals

let html = `    
    <div class='part partb4'>
    <img class='imgb4' src='imgdef/01.jpg' alt='img'>
    </div>`;
    $(html).insertBefore($('.part').eq(0));
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73