Search code examples
javascriptjqueryrecursioncountchildren

Is there a JS/jQuery Fully Recursive child node count function?


Folks,

Let's say that I have the following situation:

<div class="outer">
    <div>
        <span>
             <ul>
                 <div> </div>
                 <div> </div>
             </ul>
         </span>
     </div>
  </div>

How would I find the total number of elements inside div.outer? Something like $('div.outer').children().size() or length returns 1 (I'm looking, in this case, for 5) is there a shortcut or function to find the total element count using js/jQuery or should I write a function just for this issue? TIA.


Solution

  • var totalDescendants = $('div.outer *').length;

    and done.


    var totalDescendants = $('div.outer').find('*').length;

    would also work.