Previous Message
Next Message

Accessing CSS property values with JavaScript (was: RE:

Sent by Porter Glendinning on 8 February 2002 15:03


At 15:11 2/8/02, Ben Henick digitized these thoughts:
>Writing the values is something I've never had trouble with.
>
>It's retrieving them that's the problem...
[snip]
>What am I doing wrong?

Nothing. This is, IMHO, one of the most developer-unfriendly aspects of 
the current interaction between JavaScript and CSS. The only values 
that you can query directly from the element's style object are ones 
that are actually set in the element's style property:

     <div style="visibility: hidden;">Can't see me</div>

Or ones that you've set explicitly with JavaScript:

     var hideMe = document.getElementById("hideMe");
     hideMe.style.visibility = "hidden";

If you set the properties anywhere else, they get stored in one of the 
document's stylesheet objects, and, as far as the JavaScript interface 
to the DOM is concerned, are never directly associated with the 
element(s) to which they apply.

As a developer this is horribly broken, since it means I have to do one 
of three things:

   * Define all styles that I want to manipulate in style properties
     within each element, thus tying that style information to the
     content, making for possible maintenance nightmares.

   * Define all styles that I want to manipulate with JavaScript,
     potentially creating similar maintenance issues as above, but
     adding the problem that the browser will have to execute these
     commands after the page is loaded, meaning the page would
     potentially jiggle and flash visibly while the settings were
     being changed.

   * Write code to walk through all the stylesheet objects that apply to
     the document looking for rules that could possibly apply to the
     element I want to work with, which the browser already does in order
     to render the page in the first place, and probably does a lot
     better than I could with JavaScript anyway.

Each of these methods obviously has its problems. The first two would 
work on a fairly small scale as a bandage, since we don't really have 
much of a choice. But I wouldn't wish the third on anyone.

This will continue to be a hassle until the browsers provide a 
mechanism for querying all the style properties applied to page 
elements in one location.

- Porter

+--------------------------------------------------------+
| Porter Glendinning      [EMAIL-REMOVED]  |
| Web/UI Commando            http://www.glendinning.org  |
+--------------------------------------------------------+
| Porter's Workshop - http://www.serve.com/apg/workshop/ |
+--------------------------------------------------------+
Previous Message
Next Message

Message thread: