Change class style

Discussion in 'Javascript' started by Robert, Jun 9, 2006.

  1. Robert

    Robert Guest

    Hi,

    Is it possible to dynamically change the style of a class instead of
    just an individual element? If so could you point me in the right direction?

    Robert
     
    Robert, Jun 9, 2006
    #1
    1. Advertising

  2. Robert wrote:

    > Is it possible to dynamically change the style of a class instead of
    > just an individual element? If so could you point me in the right
    > direction?


    You can change the style(s) of a certain rule in a stylesheet with IE
    and with Netscape/Mozilla/Firefox. Opera however does not support that,
    not sure about latest Safari/Konqueror.
    There is a collection
    document.styleSheets
    for all stylesheets used (<style type="text/css"> element and <link
    rel="stylesheet">).
    With IE/Win each stylesheet (e.g.
    document.styleSheets[someIndex]
    ) has a collection
    document.styleSheets[someIndex].rules
    with Netscape/Mozilla/Firefox the name of the collection is W3C DOM
    Level 2 compliant and is
    document.styleSheets[someIndex].cssRules
    You can loop through those rules and find the rule by its selectorText
    and then change properties defined in the rule e.g.
    document.styleSheets[someIndex].cssRules[1].style.backgroundColor =
    'blue';

    Finally an inefficient way to change a class style is to add a new
    stylesheet as the last stylesheet. With Opera that is your only way e.g.
    var styleElement = document.createElement('style');
    styleElement.type = 'text/css';
    styleElement.appendChild(document.createTextNode(
    '.className { background-color: blue }'
    ));
    document.getElementsByTagName('head')[0].appendChild(styleElement);
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jun 9, 2006
    #2
    1. Advertising

  3. Martin Honnen wrote:

    > You can change the style(s) of a certain rule in a stylesheet with IE
    > and with Netscape/Mozilla/Firefox. Opera however does not support that,


    It looks as if Opera 9 is going to support document.styleSheets as the
    latest weekly Opera 9 beta build
    <http://my.opera.com/desktopteam/blog/show.dml/291397>
    has that feature.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jun 10, 2006
    #3
  4. Robert

    Robert Guest

    Martin Honnen wrote:
    >
    >
    > Martin Honnen wrote:
    >
    >> You can change the style(s) of a certain rule in a stylesheet with IE
    >> and with Netscape/Mozilla/Firefox. Opera however does not support that,

    >
    >
    > It looks as if Opera 9 is going to support document.styleSheets as the
    > latest weekly Opera 9 beta build
    > <http://my.opera.com/desktopteam/blog/show.dml/291397>
    > has that feature.
    >


    Okay, thanks!
     
    Robert, Jun 12, 2006
    #4
    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. E11
    Replies:
    1
    Views:
    4,896
    Thomas Weidenfeller
    Oct 12, 2005
  2. dlo_olb
    Replies:
    2
    Views:
    2,348
    Fredrik Lundh
    Jun 25, 2003
  3. Ken Varn
    Replies:
    0
    Views:
    504
    Ken Varn
    Apr 26, 2004
  4. torbs
    Replies:
    2
    Views:
    120
    torbs
    May 2, 2006
  5. Jayden
    Replies:
    9
    Views:
    308
    alex23
    Sep 27, 2012
Loading...

Share This Page