.innerHTML <br> breaking
Asked Answered
M

2

6

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>";
}
Misshapen answered 24/9, 2013 at 19:32 Comment(3)
what do you mean by "breaking"?Somersomers
Hope the problem is not because of \ !!Interoceptor
#805607Roose
C
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>";
}
Candlenut answered 24/9, 2013 at 19:34 Comment(0)
S
5

Javascript string literals cannot contain newlines.

You can escape the newlines with backslashes:

var myString = "a\
b";
Shluh answered 24/9, 2013 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.