0

I want a string of text to change color from default to #c30 when I click a button somewhere on the page, and it changes back to default when I click the button again.

My code looks like this:

$("#button").click(function() {
    var anno = $(#text);
    if (anno.css('color') == '#c30') {
        anno.css('color', '');
    } else {
        anno.css('color', '#c30');
    }

});

But it doesn't seem to work on FF3. Works in IE though. Any idea?

3
  • 2
    You can't check the current color of an element and expect a HEX value. In most browsers an RGB representation will be returned, so instead of '#C30' you'll get: 'rgb(204, 51, 0)' Commented Apr 25, 2009 at 9:16
  • Do not test against an RGB colour value either though, as you cannot guarantee any particular format across browsers. Commented Apr 25, 2009 at 11:14
  • if you do an alert(anno.css('color')); you will see different results in different browsers, so you can't do a text compare. Commented Jul 22, 2009 at 19:15

3 Answers 3

7

I would try to separate the presentational details as much as possible, i.e. change the classes to which the element belongs, and leave the colour information in a separate stylesheet.

$("#button").click(function() {
        $("#text").toggleClass('somethingDescriptive');
});
Sign up to request clarification or add additional context in comments.

Comments

0

If you use named colors, this will work.

$("#button").click(function(){
      $("#text").css('color', $("#text").css('color') == 'white' ? 'black' : 'white');    
});

Hex values do not.

This is a bad way to do it anyways, I agree with the most voted up answer here. So I have updated my answer after some research.

<style type="text/css">
     .color1 { color:#fff; }
     .color2 { color:#000; } 
</style>
<script>
    $("#button").click(function(){
          $("#text").toggleClass('color1').toggleClass('color2');
    });
</script?

<div class="color1">Text</div>

3 Comments

jQuery doesn't need to "know" anything - it directly applies the color you specify to the appropriate property; therefore, short HEX values are okay to use.
Also, the computed style returned from the 'css' method will rarely return the HEX value of the colour. For example, say you apply #FFFFFF to an element; when you try retrieving it you'll get rgb(255, 255, 255) ...
@JimmyP Not always true, it does not return the computed style when using named colors for example. If you view the source of jQuery in SVN, you will see that computedStyle is the last thing taken into account. Try this out to see $(function() { $(".colorMe").css('color', 'red'); alert($(".colorMe").css('color')); } I don't change things like this anymore anyways, add/removeClass is a better way to switch styles
-1

The color #c30 is converted to #cc3300 - that's why it is not working.

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.