restore original className

Discussion in 'Javascript' started by bmgz, Jun 22, 2005.

  1. bmgz

    bmgz Guest

    I have written a script that highlights a table row when the appropriate
    checkbox is checked. Using element.style is a bit messy and doesn't
    really fulfil my needs..

    I want to just be able to use className, but when I uncheck a row I am
    having difficulty restoring the original (when the page loaded)
    className, I need to do this because the rows have different styles

    Is their any other way to access the original className of an element?
    bmgz, Jun 22, 2005
    #1
    1. Advertising

  2. bmgz wrote:


    > I want to just be able to use className, but when I uncheck a row I am
    > having difficulty restoring the original (when the page loaded)
    > className, I need to do this because the rows have different styles
    >
    > Is their any other way to access the original className of an element?


    I don't think so, once you manipulate className the initial value is
    gone so you need to store it before changing it and restore it back when
    needed e.g.
    function setClassName (element, className) {
    element.previousClassName = element.className;
    element.className = className;
    }

    function unsetClassName (element) {
    if (typeof element.previousClassName == 'string') {
    element.className = element.previousClassName;
    }
    }


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jun 22, 2005
    #2
    1. Advertising

  3. bmgz

    Matt Kruse Guest

    bmgz wrote:
    > Is their any other way to access the original className of an element?


    If your original class is:

    class="myClass"

    Then when you set your new class you can do:

    ..className = .className + " myHighlightedClass";

    When you unhighlight, chop off the extra class.

    Btw, setting className directly is sometimes not a good idea, because an
    object may have several classes. Setting it explicitly destroys any other
    classes it might have.

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
    Matt Kruse, Jun 22, 2005
    #3
    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. david wolf
    Replies:
    2
    Views:
    4,176
    samra
    Dec 10, 2012
  2. Oliver S.

    classname::classname a type ?

    Oliver S., Sep 11, 2003, in forum: C++
    Replies:
    1
    Views:
    494
    White Wolf
    Sep 11, 2003
  3. Hongzheng Wang
    Replies:
    32
    Views:
    859
  4. Replies:
    0
    Views:
    544
  5. Ante Perkovic

    CSS: "tagname.classname" or ".classname"

    Ante Perkovic, Dec 22, 2003, in forum: Javascript
    Replies:
    2
    Views:
    98
Loading...

Share This Page