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); contents.splice(index,1,$('.foo').contents());
...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();