SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CSS Property Retrieving

    Hello. I cant retrieve external and embedded css properties. What am I doing wrong and how can I fix this?

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml"
    <
    head
    <
    title>Untitled Document</title

    <
    style type="text/css">
    #hour {color:red;}
    </style>

    <
    script type="text/javascript" >
    window.onload=function(){
    alert(document.getElementById("hour").style.color);
    }
    </script>
    </head>
    <body> 

    <h1 id="hour">00</h1>

    </body> 
    </html> 
    Thanks in advance.

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The .style property represents a style="..." attribute only.

    To find the computed value for a property, you can use
    Code JavaScript:
    var h = document.getElementById("hour");
    alert(document.defaultView.getComputedStyle(h, null).getPropertyValue("color"));
    This works in Gecko browsers and Opera (don't know about Safari etc.). For IE, you can use
    Code JavaScript:
    var h = document.getElementById("hour");
    alert(h.currentStyle["color"]);

    See Get Style by PPK for more info.
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •