Forum Moderators: open

Message Too Old, No Replies

Div tag height?

How to access the height of a <Div>

         

RossWal

8:52 pm on Jul 11, 2002 (gmt 0)

10+ Year Member



Please forgive this elementary question. I need to dynamically access the height of a div for the purpose of absolute placement of additional divs. Maybe I could make it work with strictly relative placement, but I'd still like to know how to do this anyway. Neither alert() in the following yields a height. Can anyone explain what I'm doing wrong?

Thanks!

<html>
<head>

</head>

<body
style="width:100%;overflow-x:hidden;overflow-y:scroll"
marginheight="0" marginwidth="0" leftmargin="0" topmargin="0"
onLoad="alert(document.all['divboatphoto'].style.height)">
<div id=divboatphoto>
<img name=boatphoto src="it.png">
</div>

<script>alert(document.all.[divboatphoto'].style.height)"
</script>

</body>
[/pre]

moonbiter

9:39 pm on Jul 11, 2002 (gmt 0)

10+ Year Member



document.all.[divboatphoto'].style.height

This is not the way to call the element. Use:

document.all['divboatphoto'].style.height

or

document.all.divboatphoto.style.height

(both work in MSIE) or:

document.getElementById('divboatphoto').style.height

(the W3C DOM way, works in MSIE 5.5 and Mozilla).

BTW, I don't know that you can pull out the height like that on a computed value. ...

RossWal

10:22 pm on Jul 11, 2002 (gmt 0)

10+ Year Member



Thanks Mooonbiter!

I had tried so many variations, and it looks like the answer is as you suggest; the height does not dynamically recalcullate as content is added to the div. Back to the drawing board for me.

Purple Martin

12:01 am on Jul 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I came up with a really ugly solution to this a long time ago. I think it went something like this:

1. Put an empty DIV or SPAN directly below your content DIV (so that it moves up and down as the content DIV shrinks and grows) and give it a unique ID.
2. Write a JavaScript function that reads the offsetTop property of your new DIV or SPAN. From this you can calculate the height of your content DIV.
3. Use a setTimeout to call your function after updating the content DIV. You need to use the setTimeout so the browser finishes redrawing the content DIV before you measure the new offsetTop property. The setTimeout can be for a very short time (5 milliseconds is enough).

I did say it was ugly!

gph

3:23 am on Jul 12, 2002 (gmt 0)

10+ Year Member



That's a good idea Purple_Martin. I'm playing with a very similair situation right now.

RossWal, this works on modern browsers. I'm try to apply it with document.body.clientHeight to get the distance from a div postioned from the bottom to a top positioned div that has varying heights.

Purple_Martins method might be easier to apply in my situation.

<html>
<head>
<title>Div Height</title>
<script>
onload = function() {
if(document.getElementById) {
alert(document.getElementById('Div').offsetHeight + " px")
}
}
</script>
</head>
<body>
<div id="Div">
content<br>
content<br>
content<br>
content
</div>
</body>
</html>

gph

4:24 am on Jul 12, 2002 (gmt 0)

10+ Year Member



Well for a blow by blow I think I'm getting closer with this:

function DivHeight() {
var TopOfBottomDiv = document.body.clientHeight-104;
var VariableHeightDiv = document.getElementById('changing_div').offsetHeight;
document.getElementById("test_move").style.top = TopOfBottomDiv+VariableHeightDiv;
return;
}

gph

4:50 am on Jul 12, 2002 (gmt 0)

10+ Year Member



Ok, judging by my last post I'd say it's time for bed. Anyway, here is where I am now. It appears to be moving "test_move" to the bottom of "changing_div" on page load. This one may work for you.

function DivHeight() {
var TopOfChangingDiv = document.getElementById('changing_div').offsetTop;
var HeightOfChangingDiv = document.getElementById('changing_div').offsetHeight;
document.getElementById('test_move').style.top = TopOfChangingDiv+HeightOfChangingDiv;
return;
}

joshie76

10:46 am on Jul 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The use of clientHeight and document.all leads me to think that you're only looking for an IE solution.

In which case you may be interested one of IE's cooler 'extra' features called Dynamic Properties.

This is basically a way of embedding Javascript calculations in CSS instructions, e.g.


<div style="position:absolute;top:expression(document.body.clientHeight/2-this.clientHeight/2);left:expression(document.body.clientWidth/2-this.clientWidth/2)">centred div tag</div>

The example above creates a page centred DIV tag that dynamically repositions itself whenever it needs to. So if the user resizes the browser the DIV will centre itself accordingly without an onresize event in sight. I know this can be done with tables and the like but it's just an example.

A similar method would be an ideal solution to your problem here as you wouldn't need to worry about calling setTimeout() to setup events that wait for changes in your DIV - IE would take care of all that for you.

Here's the reference at MSDN: [msdn.microsoft.com...]

Obviously, Dynamic Properties will only work in IE5+ but they're very nice if you're lucky enough to get the opportunity to develop with them. They can do much more than dynamically alter styles and can be set up to work with any html object or property. Ideal for developing a HTML spreadsheet or similar thick application.

gph

9:41 pm on Jul 12, 2002 (gmt 0)

10+ Year Member



As always, thank you joshie76. You never fail to show me doors I didn't know were there.

RossWal

11:57 pm on Jul 12, 2002 (gmt 0)

10+ Year Member



Thanks everyone. This gives me a lot to play with and I'm sure a workable solution will come out of it.