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

The Archive Base Latest Questions

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

I have a div id=X within that i have multiple p , what i

  • 0

I have a div id=”X” within that i have multiple p , what i want is that jquery select the first p and from that p take the first 25 words and give them the Class excerpt

$("p:first").addClass("excerpt");

This isn’t a problem but how to limit the words ?

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

    You have to go into the text node inside the p element and split up the text data, then put a new wrapper element around those words, and give that wrapper a class. You can’t add a class directly to text itself.

    jQuery doesn’t really give you much in the way of tools for manipulating text nodes so you will have to do this using some plain DOM too, eg:

    var p= $('p')[0];
    if (p.firstChild!==null && p.firstChild.nodeType===3) { // TEXT_NODE
        // work out how many characters into the text 25 words is
        var ix= p.firstChild.data.match(/^\s*(\S+\s*){0,25}/)[0].length;
        // split into two text nodes
        p.firstChild.splitText(ix);
        // put the first of them into a span
        $(p.firstChild).wrap('<span class="excerpt">');
    }
    

    This requires that the text in the element be a single direct text node. If you’ve got a situation where there are more elements inside the text, eg:

    <p>
        a b c d e f g h i j k l m n o
        <em>p q r s t u v w x y (25th word here) z</em>
    </p>
    

    then the question becomes much trickier, as you can’t wrap a <span> around just part of an element.

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

Sidebar

Related Questions

Hi have the following content within an html page that stretches multiple lines <div
I have a form and a div within that form that I am using
I have a div with multiple spans inside. The spans contain text that cannot
I have a <div> that has many other <div> s within it, each at
I have three select boxes. <div style='float: left; padding-right: 10px;'> <select size=10 name=company_id> //
I have created my first asp.net UserControl that I will use in several places
I want to append the content within my 'li' to the 'trashbin' div when
So i have a page that is gonna have multiple modal popups. Each one
I have a piece of template HTML that is returned from a Javascript function:
I have div containing multiple divs and text. The inner divs are alternately floated

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.