Re: Accessing style properties within JavaScript

Discussion in 'HTML' started by Keith Bowes, Aug 7, 2003.

  1. Keith Bowes

    Keith Bowes Guest

    wrote:
    > Hi All!
    >
    > Is it possible to access a particular property of a style rule without
    > reference to an element that uses the rule?
    >
    > The hack I have tried is this: create a dummy element and set its
    > class to match a style rule and then access the style property.
    > However, I have found that my test for this does not work (code
    > below). The hard coded <p> element displays, so does the <div> created
    > by the JavaScript - but I don't see an alert box telling me the
    > color..
    >
    > Any help would be much appreciated!
    >
    > Rob
    > :)
    >
    > <html><head>
    > <style type="text/css">
    > .test{ color:#939393; }
    > </style>
    > <script type="text/javascript" language="JavaScript">
    > function getStyle() {
    > var dummy = document.createElement ("div");
    > dummy.className = "test";
    > var dummyText = document.createTextNode("testing testing one
    > two three");
    > dummy.appendChild (dummyText);
    > var body = document.getElementsByTagName ("body") [0];
    > body.appendChild (dummy);
    > if (dummy.style.color) alert ("style color: " +
    > dummy.style.color);
    > } // end getStyle function
    > </script>
    > </head><body onLoad="getStyle();">
    > <p class="test">Testing text</p>
    > </body></html>


    The reason that doesn't work is that dummy.style.color is an empty
    string, which evaluates to false. Use:
    if (typeof(dummy.style.color) == 'string')
    Keith Bowes, Aug 7, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig G
    Replies:
    3
    Views:
    712
    Craig G
    Nov 30, 2004
  2. e n | c k m a
    Replies:
    0
    Views:
    360
    e n | c k m a
    Aug 7, 2003
  3. Lasse Reichstein Nielsen

    Re: Accessing style properties within JavaScript

    Lasse Reichstein Nielsen, Aug 7, 2003, in forum: HTML
    Replies:
    0
    Views:
    423
    Lasse Reichstein Nielsen
    Aug 7, 2003
  4. Ken Varn
    Replies:
    0
    Views:
    423
    Ken Varn
    Apr 26, 2004
  5. Replies:
    2
    Views:
    68
Loading...

Share This Page