Jamie Taylor
Jamie Taylor

Reputation: 3530

jQuery count how many divs with a class there are and put into string

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

Upvotes: 6

Views: 21758

Answers (7)

Ananda G
Ananda G

Reputation: 2539

According to the reference of jQuery Forum it can be done this way.

$('input[id*="name"]').length;

Upvotes: 0

Fermin
Fermin

Reputation: 36081

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

Using the Length property.

Upvotes: 2

user243901
user243901

Reputation:

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

Upvotes: 3

Ferid G&#252;rb&#252;z
Ferid G&#252;rb&#252;z

Reputation: 596

First option is:

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

or filter() function can be used too.

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

Upvotes: 4

Fenton
Fenton

Reputation: 250882

Like this...

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

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359786

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).

Upvotes: 3

MatTheCat
MatTheCat

Reputation: 18721

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

Upvotes: 25

Related Questions