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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:04:54+00:00 2026-06-11T16:04:54+00:00

E.g. I have a div that contains all the text like: <div class=text-container> <p>

  • 0

E.g. I have a div that contains all the text like:

<div class="text-container">
<p>
Though we welcome the idea of a smaller connector, we're miffed that Apple couldn't just adopt the semi-industry standard of Micro-USB. That would make things easier for smartphone users across the globe. Yet, even so, the smaller connector may be a smart move for the future. The 30-pin connector has been around since 2003, long before the iPhone even existed: frankly, it's a dust magnet. A smaller connector helps shave extra space to achieve a smaller phone with perhaps a bigger battery. The new connector cable will mainly be used for syncing and charging by most people who own an Apple TV or Bluetooth/AirPlay accessories.
</p>
</div>

And I would like to create something like this:

<div class="text-container">
<p>
Though we welcome the idea of a smaller connector...<a href="some-javascript-to-show-more">[show more]</a>
</p>
</div>

I guess I should get all the content of the div and then Find the first e.g. 50 character and put there a link and all the other text put in some div which will be hidden, and after the click on the link the other stuff show up.

It should be toggle-like and change the text from [show more] to [show less] if it is expanded and vice versa.

Any advice how to achieve this with plain javascript and jquery itself and without other jQuery plugins?

  • 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-06-11T16:04:55+00:00Added an answer on June 11, 2026 at 4:04 pm

    Here is another solution.

    It doesn’t simply cut the words in the middle but checks endings, punctuation, and long words.

    $(".text-container p").each(function() {
        var val = $.trim(this.innerHTML),
            parsed = val.split(/\s+/),
            cut = parsed;
    
        // for each word
        for (var i = 0, k = 0; i < parsed.length; i++) {
            k += parsed[i].length + 1;
    
            if (k - 1 > 50) {
                cut = parsed.slice(0, i);
                break;
            }
        }
    
        // if one long word
        if (cut.length == 0) {
            cut.push(parsed[0].substring(0, 50));
        }
    
        val = cut.join(" ");
    
        // if the text is long enough to cut
        if (cut.length != parsed.length) {
            this.innerHTML = val.replace(/[.,;?!]$/, "")
                + "<span>...</span> ";
    
            $("<span />")
                .css("display", "none")
                .html(parsed.slice(cut.length).join(" ") + " ")
                .appendTo(this);
    
            $("<a />", {
                href : "#",
                text : "[show more]"
            }).on("click", function(e) {
                var sh = this.innerHTML == "[show more]";
                $(this).prev("span").toggle(sh).prev("span").toggle(!sh);
                this.innerHTML = sh ? "[show less]" : "[show more]";
                e.preventDefault();
            }).appendTo(this);
        } else {
            this.innerHTML = val;
        }
    });
    

    DEMO: http://jsfiddle.net/xRuch/

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

Sidebar

Related Questions

I have a div that contains two other divs,, like this: ---------------------------- | |
I have a two-column layout. I have a #mainContainer div that contains all of
I have a div that contains some text and the div has a background
I have a div that contains a list of messages (it's a chatroom application).
I have a div that contains a ul and in each li there is
I have a div that contains 3 fields You can see the image here
(The target browser is IE8) I have a div that contains a list of
I have a wrapper div that contains arbitrary content (I don't know its length).
I have a div that is position: absolute and it contains items that get
I have the following mark up of a div that contains a form. The

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.