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>";
}
Because you are outputting to HTML, you need to think HTML. In HTML, a new line is done with the tag <br/> .
Setting the value of innerHTML lets you easily replace the existing contents of an element with new content. Note: This is a security risk if the string to be inserted might contain potentially malicious content.
The innerHTML property returns: The text content of the element, including all spacing and inner HTML tags. The innerText property returns: Just the text content of the element and all its children, without CSS hidden text spacing and tags, except <script> and <style> elements.
innerText returns all text contained by an element and all its child elements. innerHtml returns all text, including html tags, that is contained by an element.
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>";
}
Javascript string literals cannot contain newlines.
You can escape the newlines with backslashes:
var myString = "a\
b";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With