It’s a CSS problem, not a JavaScript problem. HTML collapses white space by default — this includes ignoring newlines.
Add white-space: pre-wrap
to the output div. http://jsfiddle.net/mattball/5wdzH/
This is supported in all modern browsers: https://caniuse.com/#feat=css3-tabsize