View Full Version : Resolved Reading Original Div height using JS

07-23-2009, 08:25 AM
I have a Div that goes about 5 lines, or any variable length downward. Then I set the height to 16px using

<div style='height: 16px'>a<br>c<br>d<br>e<br>g</div> works fine, but I need to find the original height before it is set using javascript.

.innerHeight reads the new value, and so does the other one I tested (I forgot what it is called).

So I thought I would set it back to it's original height by setting the style.height to null, works fine in FF, IE, and Chrome, but not Opera. I prefer a "hack-less" method, and one that works in all browsers, and the only idea I can think of is using javascript to set the height after it has been created, and read it then like:

<div id="test">a<br>c<br>d<br>e<br>g</div>
<script type="text/javascript">setHeight("test", 16)</script>

This code is pretty inefficient, a hassle, and not very elegant.

Is there an alternate way to read the original height or a cleaner and elegant way of writing the post div javascript setHeight code?


07-23-2009, 10:12 AM
In the head section:

<script type="text/javascript">
var originalDivHeight = 0;
window.onload = function() {
var testDiv = document.getElementById("test");
if (testDiv) {
originalDivHeight = testDiv.offsetHeight;
testDiv.style.height = "16px";

In the body section:

<div id="test">a<br>c<br>d<br>e<br>g</div>

Does it suit you?

07-23-2009, 06:21 PM
I'll give it a try, thanks.

I didn't know header would be JS would be executed after the body is loaded.