Pseudo-class custom filter
Write your own expression to capture text fields:
$.extend( $.expr[":"], { textnodes: function( e ) { return e.nodeType === 3; } }); $("ul li").contents(":textnodes");
Result in the following compilation:
["text","more text","wohoooo more text"]
Fiddle: http://jsfiddle.net/jonathansampson/T3MQc/
Custom method
You can also extend jQuery.fn
to provide your own method:
$.extend( $.fn, { textnodes: function() { return $(this).contents().filter(function(){ return this.nodeType === 3; }); } }); $("ul li").textnodes();
This leads to the same conclusion that we see above.
Fiddle: http://jsfiddle.net/jonathansampson/T3MQc/1/
source share