Skip to content
Advertisement

Selecting Multiple Elements height();

I am just wondering why this jQuery won’t work:

hdr = $('.header-wrapper, #top-bar, #new-showroom-header').height();

So as you can see I am trying to get the height of multiple elements and store them all within my variable. I’d expect jQuery to add all of the elements heights together to create a final value however when I console.log the variable hdr I get the height of the first element selected.

Any idea how I can select all and store them into my var?

Advertisement

Answer

Use $.each() to get total sum of height.

var hdr = 0;
$('.header-wrapper, #top-bar, #new-showroom-header').each(function () {
    hdr += $(this).height();
});

FIDDLE DEMO

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement