How to set background color in jquery

JqueryCss

Jquery Problem Overview


How to set background color of td in jQuery?

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

Thanks

Jquery Solutions


Solution 1 - Jquery

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

Solution 2 - Jquery

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

Solution 3 - Jquery

How about this:

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

Related post: https://stackoverflow.com/questions/688222/add-background-color-and-border-to-table-row-on-hover-using-jquery

Solution 4 - Jquery

Try this for multiple CSS styles:

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

Solution 5 - Jquery

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUlhas TuscanoView Question on Stackoverflow
Solution 1 - Jqueryreko_tView Answer on Stackoverflow
Solution 2 - JqueryjAndyView Answer on Stackoverflow
Solution 3 - JqueryBvdVenView Answer on Stackoverflow
Solution 4 - JqueryDaniel Hernan Vargas OsorioView Answer on Stackoverflow
Solution 5 - JqueryWaseem KhalidView Answer on Stackoverflow