Search code examples
jquerycssheightsummargin

Get sum of margin and height with jQuery


I want to get the sum of margin-top size and height of another layer.

the margin-top of .nav1 is 30px and the height of .main is 28px

I use this code:

$('.nav').css('margin-top').replace('px', '') + $('.main').outerHeight()

but the result of my sum is 3028

How can I calculate the sum of these two numbers?


Solution

  • This is because you are appending two strings, instead of adding two integers. Try this:

    var total = parseInt($('.nav').css('margin-top'), 10) + $('.main').outerHeight();