jQuery: Get the Text of Element without Child Element

jquery-logoWhile writing code in jQuery, I often use .text() method to get the content of any element from DOM. This function is straight forward, it strips off all the html element from a selected element and returns the remaining text.

So for below element the .text() method will return “A quick brown fox”.

A quick brown fox

But by the definition the .text() returns all the text inside an element. What if you have an element which contains child elements and you want text of only the selected element ignoring all the child elements?

For example: Here we have a DIV which has text “A quick brown fox” and also contain a child DIV which has text “jumps over a lazy dog!”

A quick brown fox
jumps over a lazy dog!

What if you just want “A quick brown fox” and wants to ignore all the child element? Well, here is a simple solution using jQuery.

We clone the element that you want text of, then remove all the child elements from this cloned element and then returns the text of element.

I have written a small jQuery plugin “justtext” which you can use to get the text of element and ignore all child elements. Here is the code of justtext plugin.

jQuery.fn.justtext = function() {
	return $(this)	.clone()


So the usage is just by calling justtext() method on any element.

A quick brown fox
jumps over a lazy dog!

Online Demo

Online Demo Link


  • Viet 14 April, 2011, 15:10

    An nice and useful tutorial. Thanks.

  • Name 28 April, 2011, 21:23

    this works great if you only have one orphan child, but it stops when you have multiple lines of text and they are in different areas within the container. :)

  • Alex 25 August, 2011, 20:30

    jQuery noob here. I just spent around half an hour looking for a simple solution to a simple problem I was trying to solve, but nothing really worked until this article got me on the right track. Just wanted to say thanks, is all. :)

  • jayd 13 January, 2012, 19:18

    awesome. short and sweet AND reusable. Just what I was looking for.. thanks.

  • Joedel 27 February, 2012, 10:51

    This is what I looking for.

    Thank you very much.

  • mahesh 17 April, 2012, 11:22

    Nice example, thanks for sharing. Am sure it would go in *.util of most projects :).

  • Mat 17 May, 2012, 6:35

    well done

  • Scott Donnelly 13 June, 2012, 0:46

    Thanks – simple but effective. As mahesh said, one for the the util file!

  • pranay 1 August, 2012, 18:37

    Nice example, thanks for sharing
    but how to create .justtextUpdate()

  • SM 12 October, 2012, 15:21

    Thanks man :) cute piece of code!

  • Xtyan 11 January, 2013, 1:08

    Hi, i have this:

    Source File:

    how can I get the text between “b” tags?


  • leojg 27 February, 2013, 23:27


  • ktor 7 March, 2013, 21:08

    in some cases, a trim would be needed:

    jQuery.fn.justtext = function() {
        return $.trim($(this)  .clone()
  • Amit 15 March, 2013, 15:10

    Nyc tutorial

  • Fonkie 5 June, 2013, 7:58

    How do you solve this question?

    <ul id="panel">
    <input  type="checkbox"  value="1" />one

    I don’t want to remove the input element, I just want to replace the text “one” to “first”. This is my solution using jquery:

    var ele=$("#panel").find("li:eq(0)");
    var inputEle=ele.find("input");

    Do you have a better solution?

  • Sandip Chaudhari 6 June, 2013, 10:51

    I am getting selcted text on Click but unable to get text of list box on Enter key press

      $('#listEQ li').live('click', function() {        
                    var selected = $(this).text();              
                    if (selected != "") {
                        var scripcode = selected.split("|");
                        document.getElementById('ctl00_ContentPlaceHolder1_smartSearch').value = scripcode[0];
                        document.getElementById('ctl00_ContentPlaceHolder1_hdnCode').value = scripcode[2];                    
  • MajiD 13 June, 2013, 3:06

    Thanks, very useful snippet to get rid of the inner HTML and get the text only.

  • sachin 7 January, 2014, 17:56

    nice tutorial , please send link for learn jquery , javascript..

  • Sharon 23 January, 2014, 11:07

    Thanks Viral..this helps me finish half of my Job..But how can i change the text we got and assign the updated text?

    My aim is to get the text of div check for a word replace it and assogn back :-(

  • Edoardo 23 January, 2014, 21:26


  • sln 5 February, 2014, 20:41

    thank you so much, I’ve been looking everywhere for this solution finally I got it here thanks.

  • Brandon 1 May, 2014, 1:16

    Hi great article.

    Is it possible to remove the text justtext() found?

    $(‘blah:contains(‘foo’)”).justtext().remove(); <— Doesn't work btw

  • Kevin 22 September, 2014, 15:54

    Here is my Version of the function as a getter and setter function:

    $.fn.justtext = function(newText) {
            var $children = null;
            if(newText) {
                $children = $(this)
                return $(this);
            else {
                return $.trim($(this)

Leave a Reply

Your email address will not be published. Required fields are marked *


To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]