View Full Version : Getting element attribute values returns 0 in IE7

07-09-2007, 02:41 PM

I'm basically at a loss. I'm currently writing some code to dynamically sort the elements on my page into two seperate divs, roughly half and half (or as near as can be done).

The trouble is that while simply using childNodes on a container div and utilising "offsetHeight" to get the height of each of the nodes works in FF, it returns 0 in IE7.

This is made all the more complex by the fact that if I put the following code:

function calc() {
mung = document.getElementsByTagName('div');
for (i=5;i<mung.length;i++) {

it correctly alerts me to the individual div tags and their heights if this function is called by an onmouseover event of anything on the page (eg onmouseover="calc();" in the containing Div).

FYI I have coded it so that all childNodes of the containing div are encased in <div> tags...

for (i=0;i<a.length;i++) {
a[i].outerHTML = '<div>' + a[i].outerHTML + '</div>';

So I know it's nothing to do with the elements themselves, error checking shows that the elements are definitely in the object varaibles as required, but if I change the calc() code so that it runs from code executed when the page loads (from code executed from an external .js file at the bottom of the HTML page, using the <script src=""> tag) to the following then the height returned is always 0 again:

function calc(id) {
mung = document.getElementsByTagName('div');
alert(mung[id+1].outerHTML + " height: " + mung[id+1].offsetHeight);
return mung[id+1].offsetHeight;

where ID is the value of the childNode element that it would correspond to (hence why it says ID+1, to compare the same elements).

Basically I've set this piece of code above up as a test, in reality it's just proving to me that there seems to be an issue in getting the offsetHeight of elements if you're attempting to get that value simply by running code from page load, but that it's fine if the page is loaded and you're using an event to call a function. Unfortunately I can't access the <body> tag for one reason or another so doing this from an event seems to be out of my reach, so is there anyone that could enlighten me as to why this happens?

It's all on a XHTML 1.0 strict page.