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.
It uses the Sizzle getText method:
http://sizzlejs.com/
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()
And here's the definition of getText function.
Working example: http://jsfiddle.net/cBsDN/
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.
Example: http://jsfiddle.net/XnL7H/1/
Assuming you know how to get an element in JavaScript with out jQuery.
And then you just have to use two properties that are available for each element
innerText
andinnerHTML
. So to get the text you use:Or to get HTML, you do: