views:

76

answers:

7

Is there a way in jQuery to count how many divs you have and put that number into a string

<div class="name">SOME TEXT</div>

<div class="name">SOME OTHER TEXT</div>

<div class="different">DIFFERENT TEXT</div>

So count the divs with class name and then put that into a string so the output would be this

var strNoDivs = 2

Any ideas?

Thanks

Jamie

+5  A: 
var nb = $('div.name').length;
MatTheCat
+2  A: 

var strNoDivs = $('div.name').length;

Done.

jQuery's selector syntax is based on the CSS selector syntax (which, I suppose, is only helpful information if you're already familiar with CSS selectors).

Matt Ball
+1  A: 

var noOfDivs = $('div.name').length?

Using the Length property.

Fermin
`<nit-pick>``.length` is a property, not a function. `</nit-pick>`
Matt Ball
+2  A: 
var strNoDivs = $('div.name').length.toString();
Denis
A: 

Like this...

var divisions = $("div.name");
var strNoDivs = divisings.length.toString();
alert(strNoDivs);
Sohnee
A: 

Try this:

var strNoDivs = $('.name').length;

Clicktricity
This will get all elements with a class of "name", the requirement is only for divs with a class of "name"... i.e. $("div.name")...
Sohnee
+1  A: 

First option is:

var count= $('div.name').length;

or filter() function can be used too.

var count= $('div').filter('.aaa').length;
feridcelik