jQuery get all descendant nodes including text nodes

Is there a way in jQuery to get all descendant nodes of a parent node, including text nodes, recursively? I know the $.contents() method, but as the jQuery documentation states:

the .contents() method allows us to search through the immediate children of these elements in the DOM tree

My problem is, that I have a child node that contains more (non-text) nodes, and I need instead of just the parent. I am trying some workaround now:

var contents = container.contents();
var foo = $('.foo');
var index = contents.index(foo);

...but this inserts the array instead of the individual elements. My final step would be pushing the elements to the contents array one by bone, but I'd like to know if there is a better way to do this.


I don't really understand what you are trying to do but in order to get all descendant nodes of a certain element you may use the following:

var nodes = $('*', container).addBack().contents();

DEMO: http://jsfiddle.net/5vfBg/

Need Your Help

How to stop Ruby WEBrick app from Terminal in Xubuntu

ruby bash terminal webrick xubuntu

I started learn Ruby. I tried to work with WEBrick server. After ruby index.rb command server starts. And how to stop it? I tried Ctrl+c, Ctrl+X and other. It still working. Then I pushed Ctrl+Z. B...

Want to implement multiple database in django project

python django django-models django-orm

I want to use multiple sqlite3 database for my app. I want to write some of the data(which is user log) to one database and rest of the stuff to another db. After that I want to read from both the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.