user4122820
user4122820

Reputation:

How can I change the color CSS property of an element using jQuery

How do you change CSS in jQuery? I just want the font color for "Valid VIN" to be red and this does not work:

$("#result").html(<font color="red">"Valid VIN"</font>);

Upvotes: 0

Views: 69

Answers (2)

George
George

Reputation: 36794

You would use the .css() method.

.css() can be used in one of two ways. You can pass two string parameters, a CSS property and a value, or you can pass one object parameter with key (CSS property) and value (CSS value) pairs:

$('#result').css('color', 'red');
// or
$('#result').css({color: 'red'});

If you were targeting the <font> tag inside then:

$('#result font').css('color', 'red');

Note: I should point out that the <font> tag is obsolete, so there's that.

You can also pass a function to set a CSS value:

$('#result').css('color', function(){
   return '#'+(Math.random()*0xFFFFFF<<0).toString(16); //A random colour
});

Upvotes: 7

Gabriel Intriago
Gabriel Intriago

Reputation: 149

You must change the attribute value of the font tag

$('#result font').attr('color' , 'red');

Upvotes: 1

Related Questions