Accessing current style attributes

Had a problem this morning trying to access the display property of html form elements that were hidden because one of their parent elements had the style.display property set to “none”.

<tr style="display:none"> <td><input name="foo"/></td></tr>

My script worked fine when I called

document.getElementById("moo").style.display

on the elements whose display property I had set to “none”, but when I did the same thing to the children all it returned was an empty string.

So I did some googling and found out that the style attribute actually just gives you information about the actual style attribute in the HTML tag rather than current style information about the element.

To find current style information about the element in IE you need to do this:

var elem = document.getElementById("moo");var display = elem.currentStyle.display;

The standards compliant way (which I haven’t tried yet) is something like:

document.defaultView.getComputedValue(document.getElementById('moo')).getPropertyValue('display');

Posted on 07 Oct 04 by Helen Emerson (last updated on 07 Oct 04).
Filed under Javascript