Document.write text over multiple lines

I have a script that accepts a variable length string of input from a user, usually ~ 400-3000 letters. My script does some line modification and then has to print it to the screen. Using a document record, everything appears on one line. However, I want to have ~ 100 letters for each output line, so the user doesn't have to scroll ~ 3000 letters to the right to see all the text.

The only way I could imagine is with a for loop, but maybe someone knows a better solution?

Edit:

This is the for loop I am using to achieve this

document.write("<p>",">Position: ", (lower+1) + "-" + (lower +print_screen.length), "<br/>");

// print only 70 chars per line
for(i = 0; i < print_screen.length; i += 69){
    document.write(print_screen.substring(i, i+69), "<br/>");
};

document.write("</p>");

      

+3


source to share


2 answers


You can add line breaks using replace

regex as well. This adds a break at every 100th character:



document.write(theText.replace(/(.{100})/g, '$1<br/>'));

      

+1


source


Use this CSS for your container:



.longtext {
    white-space: -moz-pre-wrap !important;  /* Mozilla, since 1999 */
    white-space: -pre-wrap;      /* Opera 4-6 */
    white-space: -o-pre-wrap;    /* Opera 7 */
    white-space: pre-wrap;       /* css-3 */
    word-wrap: break-word;       /* Internet Explorer 5.5+ */
    word-break: break-all;
    white-space: normal;
}

      

+2


source







All Articles