Reputation: 602
I'm trying to set the CSS of an element on the fly. Can I use a CSS class inside the brackets of JQuery's css()?
I've looked in http://api.jquery.com/css/ but can't find anything on it.
To be clear, rather than doing the following, with multiple CSS items:
$("div").css("background-color":"yellow", "color":"blue");
I'd like to be able to do something along the lines of the following:
$("div").css('.abc');
(where 'abc' is the class in a stylesheet that contains multiple CSS lines)
Upvotes: 4
Views: 314
Reputation: 9691
I'm not sure what you are trying to do, but :
$('div').addClass('abc')
or $('div').attr('class','abc')
$('.abc').css(...)
$('div.abc').css(...)
Upvotes: 0
Reputation: 7489
Yes, you can do this quite easily, but not with .css
. You'll want to use .addClass
.
It would look like this:
$('div').addClass('abc');
Note that you just need the class name, not the .
before it.
There is also a removeClass
, which, surprisingly enough, removes a class.
Upvotes: 2
Reputation: 38079
Use the addClass method to add a class to the object.
$("div").addClass("abc");
Upvotes: 3
Reputation: 190907
Why not use .addClass('abc')
? There is a whole suite of related functions.
Upvotes: 5
Reputation: 7759
I don't think you can. But you can use :
$("div").addClass("abc");
And here's all the class attributes that you can use in jquery : here
Upvotes: 9