user319854
user319854

Reputation: 4106

Count the number of <div>

Is it possible using jQuery to count the number of div elements?

I have this code:

<div id = "center">
  <div class ="name">text text</div>
  <div class ="name">text text text ... </div>
  <div class ="name">text ...</div>
</div>

And get number: 3

Upvotes: 8

Views: 34982

Answers (3)

IgalSt
IgalSt

Reputation: 1984

<div id="center">
  <div class="name">text text</div>
  <div class="name">
       <div class="SomeOtherDiv">bla bla</div>
  </div>
  <div class="name">text ...</div>
</div>

Usage:

$('#center div').length  // Result: 4
$('#center > div').length // Result: 3

Upvotes: 4

Huthaifa Afanah
Huthaifa Afanah

Reputation: 199

document.getElementsByTagName("div").length will give you the desired answer

Upvotes: 8

Philippe Leybaert
Philippe Leybaert

Reputation: 171734

$("#center div").length will give you the count.

Upvotes: 28

Related Questions