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
  • 13

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
  • 249,100
  • 51
  • 377
  • 410
5

Javascript string literals cannot contain newlines.

You can escape the newlines with backslashes:

var myString = "a\
b";
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964