Steve
Steve

Reputation: 602

Is it possible to use a CSS class inside the brackets of jquery's css()?

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

Answers (5)

gabitzish
gabitzish

Reputation: 9691

I'm not sure what you are trying to do, but :

  • you can add a class to a div using $('div').addClass('abc') or $('div').attr('class','abc')
  • you can set style for that class using $('.abc').css(...)
  • you can set style for the divs with that class using $('div.abc').css(...)

Upvotes: 0

yoozer8
yoozer8

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

John Koerner
John Koerner

Reputation: 38079

Use the addClass method to add a class to the object.

$("div").addClass("abc");

Upvotes: 3

Daniel A. White
Daniel A. White

Reputation: 190907

Why not use .addClass('abc')? There is a whole suite of related functions.

Upvotes: 5

Rafael Adel
Rafael Adel

Reputation: 7759

I don't think you can. But you can use :

$("div").addClass("abc");

.addClass()

And here's all the class attributes that you can use in jquery : here

Upvotes: 9

Related Questions