To ensure there is a line break after each message within the div, simply include a <br/>
tag right after each line added in your function. Just attach it at the end of the line being inserted. For example,
"<b>Message:</b> " + msgs[i].childNodes[1].firstChild.nodeValue;
will now appear as
"<b>Message:</b> " + msgs[i].childNodes[1].firstChild.nodeValue + "<br/>";
After modifying the innerHTML
of the div, every change will automatically have a line break following it.
The entire function will resemble this:
if(xhr) {
xhr.onreadystatechange = function() {
if(xhr.readyState === 4) {
if(xhr.status === 200) {
var value = xhr.responseXML;
var msgs = value.getElementsByTagName('message');
console.log("Processing ", msgs.length, "messages");
for(var i = 0; i < msgs.length; i++) {
var id = parseInt(msgs[i].getAttribute("id"));
if(lastid < id) {
lastid = id;
}
document.getElementById("msg").innerHTML += "<b>Message:</b> "
+ msgs[i].childNodes[1].firstChild.nodeValue
+ "<br/>" // REMEMBER TO INCLUDE BR TAG
+ "\n"; // Additional newline added for readability
// Helpful when logging/alerting the actual HTML
}
}
}
}
}
This will append lines of HTML to the div like so:
<b>Message:</b> message 1<br/>
<b>Message:</b> message 2<br/>
<b>Message:</b> message 3<br/>
...
When rendered in the browser, it will display as follows:
Message: message 1
Message: message 2
Message: message 3
...