Reputation: 409
I want to do this:
e.className = t;
Where t is the name of a style I have defined in a stylesheet.
Upvotes: 3
Views: 2845
Reputation:
Here is the example that add and remove the class using jQuery.
// js
$("p:first").addClass("t");
$("p:first").removeClass("t");
// css
.t {
backgound: red
}
Upvotes: 1
Reputation: 596663
Not only that works, but it's even a best practice.
You definitively want to separate the data format (xHTML) from the design (CSS) and the behaviour (javascript).
So it's far better to just add and remove classes in JS according to event while the esthetic concerns are delegated to css styles.
E.G : Coloring an error message in red.
CSS
.error
{
color: red;
}
JS
var error=document.getElementById('error');
error.className='error';
N.B :
Upvotes: 2
Reputation: 255
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';
Upvotes: 19
Reputation: 223003
Yes, that works (with the class name as a string, as jonah mentioned). Also, you can set style attributes directly on an object, using the DOM Level 2 Style interface. e.g.,
button.style.fontFamily = "Verdana, Arial, sans-serif";
where button
is (presumably) a button object. :-)
Upvotes: 2