JQuery: How to find out how many children an element has?

Antonio Moore picture Antonio Moore · Apr 18, 2012 · Viewed 8.4k times · Source

How can I use jQuery to find out how many children an element has?

Say I have the following structure:

<div id="container">
   <div id="column1">
      <div id="asset1"></div>
      <div id="asset2"></div>
   </div>
   <div id="column2">
      <div id="asset1"></div>
      <div id="asset2"></div>
   </div>
</div>

I want to find out how many children the div element: container, has. In this case it would return 2...

Answer

T.J. Crowder picture T.J. Crowder · Apr 18, 2012

Use children and length:

$("#container").children().length