Javascript – Can an element’s CSS class be set via JavaScript


I want to do this:

e.className = t;

Where t is the name of a style I have defined in a stylesheet.

Best Solution

If e is a reference to a DOM element and you have a class like this: .t {color:green;} then you want reference the class name as a string:

e.className = 't';