I think this is a pretty straightforward problem but...
var outerHeight = $('.profile').outerHeight();
$("#total-height").text(outerHeight + 'px');
Right now the var outerHeight
gives me the outerHeight of only the first element with the class .profile
.
How can I get the sum of the outerHeights of all elements with the class .profile
?
More modern option:
Try this:
You can also be lazy as me and introduce a new jQuery function that will do all the work for you, like this:
And then use it in your code wherever you like it:
For me it is also a little bit more readable and DRY if you use it often.
$("selector")
is already a collection. Access directly the.outerHeight()
or any other method like.height()
Here's the straight forward solution. Just loop through the elements of the jQuery object summing up the
outerHeight()
s.The important thing is that all jQuery getters only return the value of the first element in the jQuery set but you can add them yourself.
And here's a roundabout but cool way of doing it http://jsfiddle.net/mendesjuan/bKtAn/6/
Which could be generalized as http://jsfiddle.net/mendesjuan/bKtAn/7/
And made into a plugin like: http://jsfiddle.net/mendesjuan/bKtAn/9/
I think I went a bit overboard, sorry, I got excited, but these code snippets teach you a lot about JS and even a bit of jQuery