469,632 Members | 1,639 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

restore original className

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?
Jul 23 '05 #1
2 1781


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/
Jul 23 '05 #2
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
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Matthew | last post: by
1 post views Thread by Hakim | last post: by
9 posts views Thread by GL | last post: by
11 posts views Thread by Chris | last post: by
4 posts views Thread by bridgemanusa | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.