Reputation: 3641
I know that height()
method of jquery returns a number unitless...
How can i be sure that this number is in the unit i want?
I want to work with centimeter cm any ideas?
Upvotes: 1
Views: 3015
Reputation: 1
you can use an invisible div with a known size and then use. width() and divide by the known size to get the ratio.
Upvotes: 0
Reputation: 51
var div = $("<div></div>").css("height","1cm");
$("body").prepend(div);
var height = div.height();
div.remove();
alert(height);
I know it is stupid solution, but how about this? :D For me usually returns 38px == 1cm
Do you think it's OK?
Upvotes: 5
Reputation: 9216
height returns height in pixels. Unfortunately centimeters is going to be very difficult for you to work with. Monitors all have different pixel densities, and javascript does not have a way to know this pixel density. You'll have to estimate how many pixels average a centimeter and convert from there.
Average pixel density would have anywhere from 100-130px in a centimeter, as you can tell this is a pretty wide range.
Upvotes: 0
Reputation: 57968
the unit returned by height() is pixels.
converting that to cm might be difficult, as that depends on the user's monitor, size of pixels, etc.
question, why in gods name do you want cm?
Upvotes: 6