Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can jquery return height of an element as a percentage?

Tags:

jquery

$('#my_div').height('50%') will set the height but how can you retrieve the div's current height as a percentage?

like image 318
Dennis_M Avatar asked Aug 14 '10 05:08

Dennis_M


1 Answers

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
    );
like image 103
Ken Redler Avatar answered Oct 24 '22 02:10

Ken Redler