Reputation: 41
var test = document.getElementById('test');
console.log(test.offsetHeight);
<div style="height: 30px;" id="test">
<p>ffffffffff</p>
<p>gggggggggg</p>
<p>aaaaaaaaaa</p>
<p>dddddddddd</p>
</div>
test
div is overflow
and more than 30px
: so how to get accurate height?
Upvotes: 2
Views: 1085
Reputation: 3598
You can use either .clientHeight
or .offsetHeight;
.
What is the difference?
.clientHeight
includes padding.
.offsetHeight
includes padding, scrollBar and borders.
var clientH = document.getElementById('test').clientHeight;
var offsetH = document.getElementById('test').offsetHeight;
console.log(clientH);
console.log(offsetH);
<div style="height: 30px;" id="test">
<p>ffffffffff</p>
<p>gggggggggg</p>
<p>aaaaaaaaaa</p>
<p>dddddddddd</p>
</div>
Upvotes: 1
Reputation: 15604
var test = document.getElementById('test');
alert(test.scrollHeight);
<div style="height: 30px;" id="test">
<p>ffffffffff</p>
<p>gggggggggg</p>
<p>aaaaaaaaaa</p>
<p>dddddddddd</p>
</div>
try scrollHeight
Upvotes: 2
Reputation: 680
Try this.
var offsetHeight = document.getElementById('test').offsetHeight;
Upvotes: 0
Reputation: 1
try:
var test = document.getElementById('test');
alert(test.clientHeight);
Upvotes: 0
Reputation: 144
Try this:
window.getComputedStyle(document.getElementById('test')).height
Upvotes: 0