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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:26:43+00:00 2026-05-23T10:26:43+00:00

I see that jQuery has the .text() function for getting all text within an

  • 0

I see that jQuery has the .text() function for getting all text within an element and its descendant elements.

Is there a way to only get the text that is directly within the element?

eg.

<div>
Here is <b>some</b> text.
</div>

From that I would want to get just Here is text.

  • 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-23T10:26:44+00:00Added an answer on May 23, 2026 at 10:26 am
    var result = $('div').contents().map(function() {
        if( this.nodeType === 3 ) {
            return this.data;
        }
    }).get().join('');
    

    Example: http://jsfiddle.net/PeXue/

    This uses the contents()[docs] method to get all children of the div, including text nodes, then the map()[docs] method to build a collection of the text contents of only the text nodes (this.nodeType === 3) using their .data property.

    After that is done, it makes an Array from the collection using the get()[docs] method, and finally joins the result using .join()[docs].

    Of course your selector should be specific to the <div> you’re targeting.


    EDIT: If you want to normalize the spaces between the words a bit, you can trim the leading and trailing white space from the content of each text node using the jQuery.trim()[docs] method, then give .join() a single space to join each set.

    var result = $('div').contents().map(function() {
        if( this.nodeType === 3 ) {
            return $.trim( this.data );
        }
    }).get().join(' ');
    
    alert( result );
    

    Example: http://jsfiddle.net/PeXue/1

    We could even shorten it a bit, and ensure that any empty text nodes are excluded at the same time:

    var result = $('div').contents().map(function() {
        return $.trim( this.data ) || null;
    }).get().join(' ');
    
    alert( result );
    

    Example: http://jsfiddle.net/PeXue/2


    EDIT 2:

    You can get an enormous performance increase by using the jQuery.map()[docs] method, which is meant for more generic use.

    var result = $.map( $('div')[0].childNodes, function(val,i) {
      if (val.nodeType === 3) {
        return val.data;
      }
    }).join('');
    

    Here’s a performance test showing the difference.

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

Sidebar

Related Questions

I see that within MySQL there are Cast() and Convert() functions to create integers
I see lot of example opening popup with jquery but don't see anything that
I see that over on this question LINQy way to check if any objects
I see that there is Controller.RedirectToAction(string actionName, string controllerName, object values) but I never
I see that Flex3 has a class called ApplicationControlBar dedicated to holding components that
While googling, I see that using java.io.File#length() can be slow. FileChannel has a size()
I see that in PL/SQL it is possible to set the session state of
I see that EF can update a model based on an existing database schema.
Clicking through to the download-page if see that the last version of the download
Since VS 2005, I see that it is not possible to simply build a

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.