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 3940076
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:22:30+00:00 2026-05-20T00:22:30+00:00

I have a div with an ID shortblogpost. I would like to count up

  • 0

I have a div with an ID “shortblogpost”. I would like to count up to 27th word then stop and add “…” at the end.

I was trying the following code. Problem, Its counting letters and not words. I think its using jQuery and not strait up JavaScript?

I need to use JavaScript for various server reasons only

<script type="text/javascript">
var limit        = 100,
    text         = $('div.shortblogpost').text().split(/\s+/),
    word,
    letter_count = 0,
    trunc        = '',
    i            = 0;

while (i < text.length && letter_count < limit) {
  word         = text[i++];
  trunc       += word+' ';
  letter_count = trunc.length-1;

}

trunc = $.trim(trunc)+'...';
console.log(trunc);
</script>

Ty all in advance for any help.

  • 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-20T00:22:30+00:00Added an answer on May 20, 2026 at 12:22 am

    Truncate function.

    Use: truncate(‘This is a test of this function’, 2);
    Returns: This is…

    Use: truncate(‘This is a test of this function’, 5, ‘+++’);
    Returns: This is a test of+++

    function truncate (text, limit, append) {
        if (typeof text !== 'string')
            return '';
        if (typeof append == 'undefined')
            append = '...';
        var parts = text.split(' ');
        if (parts.length > limit) {
            // loop backward through the string
            for (var i = parts.length - 1; i > -1; --i) {
                // if i is over limit, drop this word from the array
                if (i+1 > limit) {
                    parts.length = i;
                }
            }
            // add the truncate append text
            parts.push(append);
        }
        // join the array back into a string
        return parts.join(' ');
    }
    

    Edit:
    Quick and dirty implement by parameters of OP:

    <script type="text/javascript">
    // put truncate function here...
    
    var ele = document.getElementById('shortblogpost');
    ele.innerHTML = truncate(ele.innerHTML, 20);
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi i have following code in this i have div card-208 on click i
I have the following code in my CSS file, which isn't working. #home table
i have div's wrapped under div's my html code is. <div id=content-row> <div id=left-box>
I have div with a few links spread out in the content. I would
I'm trying to create a div which will have fixed width and flexible height
I have the following HTML snippet: <body> <div class=main> <div class=topBar> <p>testing</p> </div> <div
I have Div that it contains some other elements like <p> , <ul> ,...
suppose i have div like <div id='main'> <div id='child'>my static content</div> </div> $('#main').append(<div class='mycontent'>I'm
I have div with the following css style: width:335px; float:left; overflow:hidden; padding-left:5px; When I
I have div elements and hold text/string inside them, then I try to iterate

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.