Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6992479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:38:22+00:00 2026-05-27T19:38:22+00:00

I want to split a long text into smaller chunks, that will act as

  • 0

I want to split a long text into smaller chunks, that will act as pages.

    var longText = document.getElementById('content').innerHTML;

    for (i=0; i<10; i++) {
            var page = longText.substring(i*100,(i+1)*100);
            document.write(page + "<br /><hr />");
    }

See it here on jsfiddle.

This code splits the text, but in a stupid way, cutting also words in half.
It would be far better, for example, creating substrings ending at the last space in a certain number of characters (count 100 characters, then go back to the last space).
How would you achieve it?

Second shot
Third shot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:38:23+00:00Added an answer on May 27, 2026 at 7:38 pm

    For those looking for a working answer:

    <div id="long-text">Lorem ipsum [...]</div>
    
    
        <script>
    var splitter = function(id) {
    var longText = document.getElementById(id).innerHTML;
    
    var pageLenght = 200;
    var charsDone = 0;
    var htmlBefore = "<p>";
    var htmlAfter = "</p>";
    
    while (charsDone <= longText.length && (pageLenght+charsDone)<longText.length) {
        var pageBox = longText.substr(lastSpace,pageLenght);
        var lastSpace = charsDone + pageBox.lastIndexOf(" ");
        var page = longText.substring(charsDone,lastSpace);
        document.write(htmlBefore + page  + htmlAfter);
        charsDone = lastSpace;
        }
    
        document.write(longText.substr(lastSpace,pageLenght));
    }
    
    
    splitter("#long-text");
    

    You can easily use arrays instead of writing to document.
    You will also want to set your html to your needs, do it in htmlBefore and htmlAfter.
    See it in action here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very long string that I want to split into 2 pieces.
I have a 32 bit long variable, CurrentPosition, that I want to split up
Hey, I want to split a video which is one second long (25fps)into 25
I have a long expression that I would like to split into a collection
I want to split an arithmetic expression into tokens, to convert it into RPN.
I want to split up the jQuery .js file into two, but I have
I want to split a string like this: abc//def//ghi into a part before and
We want to split our large asp.net mvc web application into multiple Visual Studio
I want to split a file containg HTTP response into two files: one containing
i want to split the searchrequest into parts, if there's nothing to find. example:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.