Adding whitespace in a Javascript document.write

DoN_Dan picture DoN_Dan · Feb 3, 2016 · Viewed 23.8k times · Source

So I'm currently creating a dynamic table using some JavaScript and a set of objects. I need to add in some white space between the two but one space isn't enough, I need to have it almost tabbed out. What would be the best way to do this? Here is my code for it so far:

var sections = {

   p1 : {sname: "Dynamic Table", mscore: 20},
   p2 : {sname: "IntelliJ Usage", mscore: 10},
   p3 : {sname: "Calender Control", mscore: 30},
   p4 : {sname: "Active Form", mscore: 20},
   p5 : {sname: "Object Database", mscore: 20}
};

document.write(Object.keys(sections).reduce(function(s, p, i) {
   var o = sections[p];
   return s + (i>0?'<br><br><br><br>':'') + o.sname + '  ' + o.mscore + ' ' }, '')
);

Answer

jros picture jros · Feb 3, 2016

If you want to add whitespace to the DOM, try using a nonbreaking space

'&nbsp;'

instead of

' '

These nonbreaking spaces can be chained.

&nbsp;&nbsp;

would force two spaces to be displayed on the page.