$('#my_div').height('50%')
will set the height but how can you retrieve the div's current height as a percentage?
You can use .height()
, which returns just the number (i.e, without the unit symbol). Perfect for percentage, which of course needs no units. From the docs:
The
.height()
method is recommended when an element's height needs to be used in a mathematical calculation.
So you can try something like this:
var height_pct = Math.round(
$('#my_div').height() /
$('#my_div').parent().height() * 100
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With