5

Why is this breaking? I've not used .innerHTML correctly before and don't know why this would be wrong.

function asdf() {
    document.getElementById("qwerty").innerHTML="A<br>
      B<br>
      C<br>
      D<br>";
}
Kevin Banas
  • 191
  • 2
  • 3
  • 12

2 Answers2

6

You have to escape new-lines in JavaScript string-literals:

function asdf() {
    document.getElementById("qwerty").innerHTML="A<br>\
      B<br>\
      C<br>\
      D<br>";
}

Though you could, potentially more-easily, simply insert newlines in the string itself:

function asdf() {
    document.getElementById("qwerty").innerHTML = "A<br>\nB<br>\nC<br>\nD<br>";
}
David Thomas
  • 240,457
  • 50
  • 366
  • 401
5

Javascript string literals cannot contain newlines.

You can escape the newlines with backslashes:

var myString = "a\
b";
SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933