12

I quickly tried to find the implementation in jQuery’s source, but only found this which doesn’t actually seem to define it completely.

From the jQuery Source

jQuery.fn.extend({
    text: function( text ) {
        if ( jQuery.isFunction(text) ) {
            return this.each(function() {
                return jQuery(this).text( text.call(this) );
            });
        }

        if ( typeof text !== "object" && text !== undefined ) {
            return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
        }

        return jQuery.getText( this );
    },

Anyone know?

Clarification:
I know how to use it. I just want to know how to get the text of an element à la jQuery when jQuery isn’t available.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
Alan H.
  • 16,219
  • 17
  • 80
  • 113
  • I edited and added the source code you refer/link to. When someone peer reviews it, it should show up. – Jared Farrish Feb 16 '11 at 23:39
  • Interesting to note that there are two different methods recommended below: The jQuery way, using Sizzle; and the native .textContent or .innerText methods — and they seem to hangle line breaks in completely opposite ways:
    ignored and whitespace preserved, or the opposite.
    – Alan H. Feb 17 '11 at 00:21
  • The current Sizzle appears to fall back to textContent but does not use innerText - http://bugs.jquery.com/ticket/11153 – Flash Aug 24 '12 at 06:29

5 Answers5

11

jQuery.fn.text can be used for 3 different purposes, as the source you pasted clearly shows. The case you're looking for, is the third one - returning the text value of an element.

jQuery uses jQuery.text() method to get the text value of an element, and jQuery.text points to Sizzle.getText()

jQuery.text = Sizzle.getText;

And here's the definition of getText function.

// Utility function for retreiving the text value of an array of DOM nodes
Sizzle.getText = function( elems ) {
    var ret = "", elem;

    for ( var i = 0; elems[i]; i++ ) {
        elem = elems[i];

        // Get the text from text nodes and CDATA nodes
        if ( elem.nodeType === 3 || elem.nodeType === 4 ) {
            ret += elem.nodeValue;

        // Traverse everything else, except comment nodes
        } else if ( elem.nodeType !== 8 ) {
            ret += Sizzle.getText( elem.childNodes );
        }
    }

    return ret;
};

Working example: http://jsfiddle.net/cBsDN/

Dogbert
  • 212,659
  • 41
  • 396
  • 397
10
var text = element.innerText || element.textContent;

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

user113716
  • 318,772
  • 63
  • 451
  • 440
  • Huh, seems this isn’t how jQuery/Sizzle do it, but that’s still really interesting and still answers the spirit of the question — thanks for sharing! – Alan H. Feb 17 '11 at 00:04
  • I wonder why it isn't like that in Sizzle...because it's obviously slower they way they do the recursive summing of texts. – vsync Jan 09 '14 at 14:36
  • innerText and textContent are not standards and work differently in different browsers. They mostly work ok, but e.g. you might end up in situation where there is a lot more space / special characters in the string than should be. As browsers format the child nodes tags in different way. – Hachi Apr 08 '16 at 08:33
2

It uses the Sizzle getText method:

// Utility function for retreiving the text value of an array of DOM nodes
Sizzle.getText = function( elems ) {
    var ret = "", elem;

    for ( var i = 0; elems[i]; i++ ) {
        elem = elems[i];

        // Get the text from text nodes and CDATA nodes
        if ( elem.nodeType === 3 || elem.nodeType === 4 ) {
            ret += elem.nodeValue;

        // Traverse everything else, except comment nodes
        } else if ( elem.nodeType !== 8 ) {
            ret += Sizzle.getText( elem.childNodes );
        }
    }

    return ret;
};

http://sizzlejs.com/

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
0

If you know the different of createTextNode and createElement, you can understand how to jquery.text work.

text function create a DOM text node. The browser will tread the value of node as text.

xfygx
  • 1
0

Assuming you know how to get an element in JavaScript with out jQuery.

var el = document.getElementById("my-element");

And then you just have to use two properties that are available for each element innerText and innerHTML. So to get the text you use:

var text = el.innerText;

Or to get HTML, you do:

var html = el.innerHTML;
Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
  • Thanks for the tip. FWIW, patricks's suggestion would be more cross-browser, an innerText is not supported by Firefox: http://www.quirksmode.org/dom/w3c_html.html – Alan H. Feb 17 '11 at 00:10