Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate height of div in jQuery?

I am in such a situation that I have to set height of a div depends on the amount of content in it, I can't give min-height in it. I know we can calculate height of div by :

$("#divid").height();

So is there any way we can calculate height of a div without having height parameter in it and give height to that div depends on the amount of content in it.

I hope I explained it clearly.

Thanks

like image 765
Ravinder Singh Avatar asked Dec 26 '22 12:12

Ravinder Singh


1 Answers

Your wording is a bit confusing, but I think you are asking how to calculate the size of the content inside a div even if the div has a different height applied. For that you can use the element's scrollHeight.

$('#divid')[0].scrollHeight

If you just want to know how to calculate the height of an element even though it has no attribute/style for height, you can just use what you have written in the question. .height() computes the height of the element directly from the browser rendered values, not from the attributes specifically.

like image 56
loganfsmyth Avatar answered Dec 29 '22 01:12

loganfsmyth