Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select text nodes with jQuery?

I would like to get all descendant text nodes of an element, as a jQuery collection. What is the best way to do that?

like image 228
Christian Oudard Avatar asked Nov 18 '08 13:11

Christian Oudard


People also ask

Is text () a jQuery method?

jQuery text() MethodThe text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text content of all matched elements (HTML markup will be removed). When this method is used to set content, it overwrites the content of ALL matched elements.

How do I get text in jQuery?

Get Content - text(), html(), and val() Three simple, but useful, jQuery methods for DOM manipulation are: text() - Sets or returns the text content of selected elements. html() - Sets or returns the content of selected elements (including HTML markup) val() - Sets or returns the value of form fields.

How do I select something in jQuery?

The select() method is an inbuilt method in jQuery which is used when some letters or words are selected (or marked) in a text area or a text field. Syntax: $(selector). select(function);

How do I select a specific tag in jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.


1 Answers

jQuery doesn't have a convenient function for this. You need to combine contents(), which will give just child nodes but includes text nodes, with find(), which gives all descendant elements but no text nodes. Here's what I've come up with:

var getTextNodesIn = function(el) {     return $(el).find(":not(iframe)").addBack().contents().filter(function() {         return this.nodeType == 3;     }); };  getTextNodesIn(el); 

Note: If you're using jQuery 1.7 or earlier, the code above will not work. To fix this, replace addBack() with andSelf(). andSelf() is deprecated in favour of addBack() from 1.8 onwards.

This is somewhat inefficient compared to pure DOM methods and has to include an ugly workaround for jQuery's overloading of its contents() function (thanks to @rabidsnail in the comments for pointing that out), so here is non-jQuery solution using a simple recursive function. The includeWhitespaceNodes parameter controls whether or not whitespace text nodes are included in the output (in jQuery they are automatically filtered out).

Update: Fixed bug when includeWhitespaceNodes is falsy.

function getTextNodesIn(node, includeWhitespaceNodes) {     var textNodes = [], nonWhitespaceMatcher = /\S/;      function getTextNodes(node) {         if (node.nodeType == 3) {             if (includeWhitespaceNodes || nonWhitespaceMatcher.test(node.nodeValue)) {                 textNodes.push(node);             }         } else {             for (var i = 0, len = node.childNodes.length; i < len; ++i) {                 getTextNodes(node.childNodes[i]);             }         }     }      getTextNodes(node);     return textNodes; }  getTextNodesIn(el); 
like image 177
Tim Down Avatar answered Sep 20 '22 11:09

Tim Down