restore original className

B

bmgz

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?
 
M

Martin Honnen

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;
}
}
 
M

Matt Kruse

bmgz said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top