Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add css font color with jquery

Sure it's a simple question, but I can't fix it, can somebody help me?

This is the original line

$('.winning-col', this).text($('td.win', this).length);

and this is where I came up with, surely not correct.

$('.winning-col', this).text.css('color', 'pink'($('td.win', this).length));
like image 259
Chris Avatar asked Aug 30 '09 01:08

Chris


People also ask

How can I change the text color with jQuery?

To change the text color with jQuery, use the jQuery css() method. The color css property is used to change text color.

Can we add CSS in jQuery?

jQuery has several methods for CSS manipulation. We will look at the following methods: addClass() - Adds one or more classes to the selected elements.

What is the use of CSS () method in jQuery?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.

What is addClass and Removeclass in jQuery?

The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.


2 Answers

You could do it the quick-way:

$(".winning-col", this)
  .text($("td.win", this).length)
  .css("color", "pink");

But ideally, you would use .addClass instead:

$(".winning-col", this)
  .text($("td.win", this).length)
  .addClass("hilighted");

Where

.hilighted { color: pink; }
like image 196
Sampson Avatar answered Oct 21 '22 19:10

Sampson


Thought I'd add a little extra information here, just in case you're not aware of it. When you use the .css() function, you can also specify the arguments as what's called an object literal, which basically means something in this format:

{objectVarName1: objectVarValue1, objectVarName2: objectVarValue2}

You can also do it like this:

{"objectVarName1": objectVarValue1, "objectVarName2": objectVarValue1}

With the .css() function, you can do this:

$("#the_item_id").css({backgroundColor: "#333", color: "#FFF"});

If the variable names you're passing aren't in quotes, you have to do it in camel-case like I did above, which means that the first word of the name of the CSS property is lowercase, but every word after that is in caps (so the CSS property background-color becomes backgroundColor). To do the equivalent of the above in the form where you put the name of the variable in the object in quotes, you would just do this:

$("#the_item_id").css({"background-color": "#333", "color": "#FFF"});

Just wanted to point out that you don't have to chain multiple calls to .css() together, that you can do all of your CSS changes at once. ;)

like image 24
John Nicely Avatar answered Oct 21 '22 20:10

John Nicely