0

In my below coe the console.log fires, so the if statement is definitely passing. If I try to console.log() the CSS of $(this) I get undefined. I've done console.log($this) and it returns an object with outer HTML <span class="formState"></span>. I've had a few friends look at this and have done a lot of research but have come up short.

<span class="formState"><?php echo $IRFormResults[$count];$count++ ?></span>
$(".formState").each(function() {
    if ($(this).text() == 'n/a') {
        console.log('Got an n/a');
        $(this).css('color:red');
    }
.formState {
    color: green;
    display: inline-block;
    min-width: 125px;
    margin-left: 10px;
    float:right;
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
dyllandry
  • 113
  • 1
  • 11

3 Answers3

5

Your syntax isn't quite right. The css() method takes two parameters; the CSS property and its value:

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

Also note that you can amend your logic to use a filter() and also to trim() the text of the element to save any whitespace causing problems:

$(".formState").filter(function() {
    return $(this).text().toLower().trim() == 'n/a';
}).css('color', 'red');
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Alright, you're totally right. Thanks so much. Though, how would I approach printing out its existing css? Just incase I wanted to. – dyllandry Nov 12 '15 at 20:16
  • It's actually quite complicated to get all the CSS rules on an element because there are hundreds of default settings applied by the browser before you put any styling on it yourself. See [this answer](http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element) for more details. – Rory McCrossan Nov 12 '15 at 20:17
1

You should change this:

$(this).css('color:red'); 

To this :

$(this).css('color','red');
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

Either

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

or

$(this).css({'color':'red'});

void
  • 36,090
  • 8
  • 62
  • 107