0

I want to get the count of some divs. Here is my page structure as a picture. I want to get the count of cityUnderText divs.

enter image description here

I am using these script codes but I m getting 0 but I need just 6.

var h = $("div.cityUnderText").length;
alert(h);

var h = $(".cityUnderText").length;
alert(h);
Mtok
  • 1,600
  • 11
  • 36
  • 62

3 Answers3

1

Try $(".cityundertext").length.

JavaScript is a case-sensitive language.

VisioN
  • 143,310
  • 32
  • 282
  • 281
1
var count=$(".cityundertext").size(); // jquery's method

OR

var count=$(".cityundertext").length; 

Or be more specific

var count=$("#content_pnlDestinations .cityundertext").size(); // jquery's method

OR

var count=$("#content_pnlDestinations .cityundertext").length; 
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Well, thanks but isn't it also available to native javascript ? Or does jquery has it's own. – The Alpha May 05 '12 at 22:16
  • Native arrays have a `length` property, and so does the jQuery object (which disguises itself as an array). – bfavaretto May 05 '12 at 22:28
  • So I'm just assuming that jQuery use `size()` method to return the `length` property of an array and `length` belongs to native javascript, this is my understanding. Is this wrong, anyway ? Thanks. – The Alpha May 05 '12 at 22:33
  • 1
    `length` is just a property name, what belongs to native JavaScript is the array object. I don't know how jQuery implements its `size` method, but it probably just returns the current object's own `length` property (no arrays involved). See [this question](http://stackoverflow.com/questions/6599071/array-like-objects-in-javascript) for a discussion on array-like objects in js. – bfavaretto May 05 '12 at 22:44
  • 1
    You're right. `size()` just returns `length` [jsapi.info](http://jsapi.info/jquery/1.7.1/jQuery.fn.size) – Andreas May 05 '12 at 22:52
0

You can use document.querySelectorAll('div > div').length, which is faster than jQuery, because it's native (2x to 3x faster).

bfontaine
  • 18,169
  • 13
  • 73
  • 107