Yusuf Ali Siddiqui
Yusuf Ali Siddiqui

Reputation: 63

How do I subtract pixels from numbers in JavaScript?

I need to change the height of the footer by finding out the height of another div.

I'm using the .css("height") function, but I can't get the function to return the answer in pixels.

Here's the code I'm using. It's returning NaN.

if($("#middleContainer").height() > 500){
  var add_height = $("#middleContainer").css("height") - 500;
  alert(add_height);
}

After looking at the suggestions, I'm using the following code. It doesn't make any difference.

if($("#middleContainer").height() > 500){
  var add_height = $("#middleContainer").height() - 500;
  var new_height = -850 + add_height;
  alert(new_height);
  $(".footer").css("top", "new_height");
}

Upvotes: 2

Views: 4732

Answers (3)

Sadikhasan
Sadikhasan

Reputation: 18600

Following code return height with string type so you have to parse as Int

var add_height = parseInt($("#middleContainer").css("height")) - 500;
// Return as String data type value

This code return number so do not want to parse as Int it directly works

var add_height = $("#middleContainer").height();
// Return as Number data type value

Upvotes: 2

Kup
Kup

Reputation: 884

The answer is always in pixeis, you just need to parse it

var h =$(something).height() - 500;
$('something').height(h);

Upvotes: 0

user4227915
user4227915

Reputation:

This should work:

parseInt( $("#middleContainer").css("height"));

Upvotes: 0

Related Questions