Tomáš Vavřinka
Tomáš Vavřinka

Reputation: 633

jQuery count height of elements with specific class

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

Answers (3)

Wils
Wils

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

Taki
Taki

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/

https://api.jquery.com/each/

Upvotes: 3

Iris
Iris

Reputation: 1476

Here is an example :

$('.a').height() * $('.a').length

Upvotes: 0

Related Questions