JQuery: How do I know how many children have an item?

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

Let's 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 has: container, has. In this case, it will return 2 ...

+6
source share
2 answers

Use children and length :

 $("#container").children().length 
+19
source

Use the direct children switch (>) and the length property:

 $('#container > *').length 

Example - http://jsfiddle.net/TtV8d/

+8
source

Source: https://habr.com/ru/post/913508/


All Articles