I am trying to divide two variables in Jquery as follows:
var image_width = parseInt($object.width());
var image_height = parseInt($object.height());
var image_ratio = image_width/image_height;
However, when I attempt to use image_ratio in an if statement…
if (image_ratio < 1) //image is taller than it is wide
{
//do something...
}
And I know that the image is taller than it is wide but the function is not entering the if statement. Is this because when two int’s divide it generates an int? If so, how can I get decimal values in order to check if the ratio is less than 1?
Thanks!
what is the result of
alert($object.height())? If it’s possible that $object is an empty jquery collection, then.height()will returnnull, andparseInt(null)will returnNaN, giving youNaNforimage_ratio. Any comparison betweenNaNand another value will always return false, which would explain the behavior you’re seeing.if
$object.height()is returning0, you’ll also getNaNforimage_ratio, and then you need to look at your jQuery selection. Are you selecting the correct element? What does$object[0].heightgive you?