View Full Version : newbie. Don't understand why CSS styles don't set DOM properties

Roy Gardiner
01-14-2004, 12:40 PM
<style type="text/css">
a {display: none;}
</style> then in a function
var allLinks = document.getElementsByTagName("A")
then in a loop of all links:
alert("88>" + allLinks[i].style.display + "<<")
allLinks[i].style.display = "block"
alert("99>" + allLinks[i].style.display + "<<") For the first Alert the display property has no value, but I'd expect "none"; what's the reason? (Note setting "block" does make all the links visible). I'm using IE 6.0.2 FWIW.

Roy Sinclair
01-14-2004, 04:04 PM
You aren't seeing the results of what a "class" applies to individual elements because the CSS isn't applied to the individual elelements.

Remember the "C" in CSS means "Cascading" which implies some levels.

Remember also that you can apply styles at the parent level, class level and then the individual level. In order to do that you've got to be able to keep separate locations for those values so in this case, since you've set "display" at the class level, looking for it's value at the individual item level isn't going to give you a result.

01-14-2004, 05:25 PM
[object HTMLElement].style reflects properties set in the style attribute. Not actually applied styles.

Use some DOM2 CSS:

document.defaultView.getComputedStyle([object HTMLElement], "").getPropertyValue("display")

And IE-proprietary fudgery:
[object HTMLElement].currentStyle.display