Search code examples
javascriptlinenewline

Javascript new line - end of document.write


I'm working on a little program, and all I need now is to make a new line between my document.write outputs. I've heard about \r and \n but it doesn't seem to work for me.

        ...
        group[j] = elev[r] + " og " + elev[k];
        document.write(group[j] += "\n");
        ...

The code over this text, is in a loop, and lots of (11) group[j]'s will be written in the document. I'm using a separate Javascript-file so I can't use the </br> (I don't think so)


Solution

  • If you want a new line to appear in your rendered HTML then you will definitely need to write a <br/> tag; whitespace is collapsed in HTML and newlines are considered whitespace

    ...
    group[j] = elev[r] + " og " + elev[k];
    document.write(group[j] += "<br/>\n");
    ...
    

    will suffice. The \n is not strictly necessary, but it will keep your resulting HTML looking neater, should you need to inspect the generated source.