Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(this).css not changing css values

I have this code

$('#downvotebutton').click(function() {
    $(this).css('background-color', 'red !important');
    console.log('foo');
    //more

When downvotebutton is clicked, 'foo' is returned to the console but the background-color doesn't change. jQuery is linked to and works elsewhere in the page. what could possibly be the cause?

like image 953
user3324865 Avatar asked Dec 09 '22 09:12

user3324865


2 Answers

Do you really need !important? Following will work:

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

Or if you need !important:

$(this).css("cssText", "background-color: red !important;");
like image 197
Sergio Avatar answered Dec 21 '22 04:12

Sergio


You cannot use !important statement using jQuery css() method.

You could use:

$(this).attr('style', function(_, rules) { 
     var newval = rules?rules:"";return newval + ';background-color: red !important;' 
});
like image 25
A. Wolff Avatar answered Dec 21 '22 05:12

A. Wolff