Advertisement
Advertisement


How to set background color in jquery


Question

How to set background color of td in jQuery?

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

Thanks

2018/08/22
1
127
8/22/2018 4:10:30 PM

Accepted Answer

$(this).css('background-color', 'red');
2011/01/24
278
1/24/2011 9:58:47 AM

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()

2018/08/22

How about this:

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

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

2017/05/23

Try this for multiple CSS styles:

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

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

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

Source: https://stackoverflow.com/questions/4780670
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]