Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 396
Jamie Taylor Avatar asked Oct 07 '10 14:10

Jamie Taylor


2 Answers

var nb = $('div.name').length;
like image 98
MatTheCat Avatar answered Oct 13 '22 21:10

MatTheCat


First option is:

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

or filter() function can be used too.

var count= $('div').filter('.aaa').length;
like image 33
Ferid Gürbüz Avatar answered Oct 13 '22 22:10

Ferid Gürbüz