SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DIV object styles in Javascript

    //HTML Markup
    <style>
    .divStyle
    {
    color:red;
    }
    </style>
    <div class="divStyle">HELLO</div>


    How to catch the current style object of a DIV tag in javascript.
    For IE, we can use currentStyle property of a DIV object.....
    But for mozilla browser its not working....

    Please help me on this.

    Thanks & Regard
    Adil

  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)
    Code JavaScript:
    var div = ... // assign the DIV's Element node
    var style = document.defaultView.getComputedStyle(div, null);

    Should work in Gecko browsers, Opera and Safari, I believe.
    Birnam wood is come to Dunsinane

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Note that getComputedStyle is not the same as currentStyle, as currentStyle will tell you something has a width of "auto" for example, but getComputedStyle will tell you the width in pixels, which is much more useful.

    Also note that when you use getComputedStyle, you then need to use getPropertyValue with it:
    Code Javascript:
    var div = ... // assign the DIV's Element node
    var color = document.defaultView.getComputedStyle(div, null).getPropertyValue('color');


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
  •