0

I'm trying to get the height and the margin-bottom of a element, and count them up. But so far, i can only do this:

var sum = $('div').height() + 25); 

That 25 i had to look up myself in the stylesheet at

div {
   height: 75px;
   margin-bottom: 25px;
}

How can i make this automaticly?

Because when i try:

var sum = $('div').height() + $('div').css('margin-bottom'); 

It isnt returning any value.

1

3 Answers 3

3

You're probably looking for http://api.jquery.com/outerheight/ which includes the margins, padding and borders (all which count towards the total height, along with the base height). Alternatively, you can use parseInt($('div').css('margin-bottom')) because the value is a css string that looks something like this: 100px. parseInt() would extract the 100 from that css, which is what you want.

In addition, you're kinda doing it wrong in general. Are you looking for the sum of all the heights of all the div elements? Or does your page have only one div?

Sign up to request clarification or add additional context in comments.

1 Comment

FYI: If you want to sum the height()s (or outerHeight()s) of all the divs, you should probably look at jquery's each (api.jquery.com/each)
0

$('div').css('margin-bottom') will have the px attached. Try and use something like parseInt()

var sum = $('div').height() + parseInt($('div').css('margin-bottom'), 10); 

W

Comments

0

There are many 'div' elements in a document(usually). You need a better selector for the one you want. Fiddle for value in console.log

<div id="TestId" class="test"></div>

.test
{
height:40px;
margin:5px;
}

var tester = $("#TestId");
var eleHeight = tester.outerHeight();
var marginBottomHeight = tester.css('margin-bottom').replace("px", "")
console.log(eleHeight + parseInt(marginBottomHeight));    

1 Comment

It was an example... :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.