Reputation: 3530
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
Reputation: 2539
According to the reference of jQuery Forum it can be done this way.
$('input[id*="name"]').length;
Upvotes: 0
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
Reputation: 250882
Like this...
var divisions = $("div.name");
var strNoDivs = divisings.length.toString();
alert(strNoDivs);
Upvotes: 1
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