Search code examples
javascripthtmlwhitespacedocument.write

Adding whitespace in a Javascript document.write


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 + ' ' }, '')
);

Solution

  • 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.