Ulhas Tuscano
Ulhas Tuscano

Reputation: 5620

How to set background color in jquery

How to set background color of td in jQuery?

e.g $(this).css({**BackgroundColor:Red**})

Thanks

Upvotes: 151

Views: 384827

Answers (5)

Waseem Khalid
Waseem Khalid

Reputation: 39

You can add your attribute on callback function ({key} , speed.callback, like is

$('.usercontent').animate( {
    backgroundColor:'#ddd',
},1000,function () {
    $(this).css("backgroundColor","red")
});

Upvotes: 0

Try this for multiple CSS styles:

$(this).css({
    "background-color": 'red',
    "color" : "white"
});

Upvotes: 6

jAndy
jAndy

Reputation: 236152

You actually got it. Just forgot some quotes.

$(this).css({backgroundColor: 'red'});

or

$(this).css('background-color', 'red');

You don't need to pass over a map/object to set only one property. You can just put pass it as string. Note that if passing an object you cannot use a -. All CSS properties which have such a character are mapped with capital letters.

Reference: .css()

Upvotes: 71

BvdVen
BvdVen

Reputation: 2961

How about this:

$(this).css('background-color', '#FFFFFF');

Related post: Add background color and border to table row on hover using jquery

Upvotes: 17

reko_t
reko_t

Reputation: 56450

$(this).css('background-color', 'red');

Upvotes: 323

Related Questions