1

I know this is an old question, and similar questions (solved) are even more complicated. But I've tried and couldn't figure it out myself.

I have

<div style="color:white; padding:10px">Text</div>

I want to change its color to red:

div[style*="color:white"]{color:red}

$('div').filter(function() {
    return $(this).css('color') == 'white';
}).css("color", "red");

I tried both css and javascript, with or without whitespace, use either hex or RGB color code.

2
  • I think your return statement is just returning true and not the element. anyway, what type of error are you getting? Commented Jan 6, 2013 at 2:31
  • Have you tried console.log or alert? You'll immediately see what's happening...console.log($(this).css('color')) Commented Jan 6, 2013 at 2:33

5 Answers 5

3

The issue is that it's not returning what you're expecting. It's actually returning an RGB string rgb(255, 255, 255), rather than 'white'. Change that and you're golden.

Example JSFiddle

Sign up to request clarification or add additional context in comments.

Comments

2

Actually $(this).css('color') returns rgb(255, 255, 255) so you can use

$('div').filter(function() {
    return $(this).css('color') == 'rgb(255, 255, 255)';
}).css("color", "red");

Example.

1 Comment

Thank you very much! (but I'll mark Apropos's answer my T-up, because he was 10s faster ^_^)
1

In JQuery API document of .css(), the following sentence is written.

"Different browsers may return CSS color values that are logically but not textually equal, e.g., #FFF, #ffffff, and rgb(255,255,255)."

So, I think the simply return $(this).css('color') == 'rgb(255, 255, 255)'; may also has some problem in the future.

And here I suggest use css class to implement it as the following:

.white{color:white;}
.red{color:red;}

$('div').filter(function() {
    return $(this).hasClass('white');
}).removeClass('white').addClass('red');

And why div[style*="color:white"]{color:red} does not work, this is because inline css has the highest priority out of the three ways.

1 Comment

Thank you very much. Fortunately, I want to do something for me only. and I can't change the html.
0

Inline CSS takes over any internal or external stylesheet. If you have the ability to remove the inline css on that particular element and replace it with something like <div class="myElememt"></div> that would be ideal. As for using jQuery I got it to change by using the CSS method without the filter:

$('div').css('color', 'blue');

Comments

0

You're doing something very funky there, the filter function shouldn't be used like that. To select a dom element and change its css with jquery do this $('div').css({'color': 'red'});

Here's a working example

For a proper introduction to jquery I'd recommend this video by the amazing Chris Coyer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.