Reputation: 633
Can't figure out this trivial thing. Have several elements with different classes and I need to count height
of that one which has for example class .a
.
<div>
<div class="a"></div>
<div class="b"></div>
<div class="a"></div>
<div class="b"></div>
<div class="a"></div>
<div class="b"></div>
</div>
I would need to get the result count in variable count_of_a
so something like that:
var count_of_a = jQuery( ".a" ).each(function(){
jQuery(this).height();
});
Upvotes: 0
Views: 990
Reputation: 1231
$(function(){
$('div.a').each(function(){
alert($(this).height());
});
});
Do you mean you want the height value total or respectively? Here's an example shows individual height of class a div.
Upvotes: 0
Reputation: 17654
you're close :)
the definition of each
is
Function( Integer index, Element element )
so you need the second argument to access your current object
try this :
var totalHeight = 0;
jQuery('.a').each(function(index ,element ){
totalHeight += jQuery(element).height();
});
console.log(totalHeight);
here's a fiddle : https://jsfiddle.net/uduauxrg/8/
Upvotes: 3