Let’s say I have a JQuery object which has (or points to) such a structure:
<div id="parent">
<div id="child1">
<div id="grandchild1">
// ... grandchild can also have children
</div>
// ... and so on of grandchildren
</div>
<div id="child2">
</div>
// .... and so on
</div>
Is there possibility to get plain array of elements (JQuery object) from my object like this:
['parent', 'child1', 'child2', ..., 'grandchild1', ...]
Thanks
P.S. Please notice that it can’t be selector something like this $(‘div > div’), because I already have
JQuery object and only from this object I need take stuff.
You can retrieve children and grandchildren using the
*selector. The items will be returned in tree order. You can then get a plain array of elements usingjQuery.toArray():Following is an overkill but useful if you are more interested in iterating the hierarchy using recursion: