-3

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"?

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 Answers5

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
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
0
console.log($('#main_div div.time').length)
Janaki
  • 185
  • 12