I have a main
Div of id = "main_div"
and in this parent div I have 3 div of class = "time"
then how to get the number of divs class = "time"
in the div of id = "main_div"
?
Asked
Active
Viewed 565 times
-3

Mahmoud Gamal
- 78,257
- 17
- 139
- 164

manishjangir
- 1,605
- 6
- 24
- 27
-
possible duplicate http://stackoverflow.com/questions/250688/count-immediate-child-div-elements-using-jquery – kbvishnu Mar 26 '12 at 06:35
5 Answers
1
console.log($('#main_div div.time').length)
The jQuery selector will return an array with all the divs, and .length property will return the number of elements in the array.

Ikke
- 99,403
- 23
- 97
- 120
-
1It is in javascript, but with jQuery. look at Kooilinc answer for pure javascript. – NeeL Mar 26 '12 at 06:35
-
1
-
@manishjangir — That **is** JavaScript. It makes use of the jQuery library (which should be acceptable since you tagged your question with it!) and uses `console.log` for output demo purposes. – Quentin Mar 26 '12 at 06:36
-
@manishjangir jQuery is a javascript library. It makes such stuff easy, whereas you would otherwise need an icky bunch of forloops. – Manishearth Mar 26 '12 at 06:37
-
@Manishearth — You don't need for loops to solve this problem without a library. – Quentin Mar 26 '12 at 06:39
-
-
-
@Quentin: Wait, that exists? I've been out of the loop for a while--IIRC one had to use a custom version beforehand. – Manishearth Mar 26 '12 at 06:46
1
Use querySelectorAll
. It returns a nodeList. The number of divs with className 'time' within div#main_div:
var nOftimeDivs = document.querySelectorAll('#main_div .time').length;

KooiInc
- 119,216
- 31
- 141
- 177
0
try this
jquery
$('div#main_div').find('div.time').length
pure javascript
document.getElementById("main_div").getElementsByClassName('time').length

dku.rajkumar
- 18,414
- 7
- 41
- 58
0
You can use the .size() or length to get the number of matched elements
$("#main_div").find("time").length

Checksum
- 3,220
- 3
- 23
- 24