Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript, trying to add linebreak inside create text node method

Tags:

javascript

This is my script:

<script>
/*jslint browser:true */
var i;
var out = document.getElementById("output");
var args = ["aaa", "bbb", "ccc", "ddd", 1, 2, 4 + 4];
function displayArgs() {
    "use strict";
    for (i = 0; i < args.length; i++) {
        out.appendChild(document.createTextNode(args[i] + "<br>"));
    }
}

displayArgs(args);
</script>

my output so far all appears on a single line with the break tags showing rather than b

like image 865
jimeast Avatar asked Oct 24 '12 00:10

jimeast


1 Answers

document.createTextNode prevents text from being rendered as html. Try this instead.

/*jslint browser:true */
var i;
var out = document.getElementById("output");
var args = ["aaa", "bbb", "ccc", "ddd", 1, 2, 4 + 4];
function displayArgs() {
    "use strict";
    for (i = 0; i < args.length; i++) {
        out.appendChild(document.createTextNode(args[i]));
        out.appendChild(document.createElement("br"));
    }
}
displayArgs(args);

Demo: http://jsfiddle.net/LVm9z/

like image 130
Larry Battle Avatar answered Sep 23 '22 12:09

Larry Battle