Lakhae
Lakhae

Reputation: 1899

How to add two heights property in jQuery?

Is it possible to add two heights properties came from two different elements? For e.g.

var reportHeight = $("#" + loadTo).css("height");
var userCompaniesHeight = $("#UserCompanies").css("height");

// is it possible --> var totalHeight = reportHeight + userCompaniesHeight;

TIA

Upvotes: 0

Views: 46

Answers (3)

Jonathan Marzullo
Jonathan Marzullo

Reputation: 7031

try this .. the jQuery CSS() method returns a string so you need to use parseInt():

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

var reportHeight = parseInt( $("#" + loadTo).css("height"), 10),
    userCompaniesHeight = parseInt( $("#UserCompanies").css("height"), 10),
    totalHeight = reportHeight + userCompaniesHeight;

or use the jQuery height() method instead as described above

Upvotes: 1

Kierchon
Kierchon

Reputation: 2299

Just use the height() method or else you have the parse out the "px" from what .css() returns

var totalheight= $("#" + loadTo).height() + $("#UserCompanies").height();

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318598

Use .height() which returns a plain number in pixels instead of a string containing units.

var reportHeight = $("#" + loadTo).height();
var userCompaniesHeight = $("#UserCompanies").height();
var totalHeight = reportHeight + userCompaniesHeight;

Upvotes: 4

Related Questions