2
function toggleLinkSelection(link){
    if($(link).css('border-top-color') == 'red'){
        $(link).css({'border-top-color': 'transparent'});
    } else {
        $("div[id$='OptionsLink']").css({'border-top-color': 'transparent'});
        $(link).css({'border-top-color': 'red'});
    }
}

Am I doing something wrong in my if statement? It never tests true. In the browser I inspect item and it should test true. I've tried replacing red with #F00 and #FF0000 and that doesn't help. The link variable = #testID Thanks.

1
  • 1
    Umm...try console.log($(link).css('border-top-color')) and see what you get? Commented Nov 26, 2011 at 5:04

2 Answers 2

4

colors are returned in rgb

$(link).css('border-top-color')==='rgb(255, 0, 0)'

ps: always use 3 equals when you know what the output will be, or you could end up with some nasty surprises

EDIT: demo by JesseB (see post below)

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

3 Comments

Thanks. I did try the rgb thing. My mistake was I needed a space after the commas like so.... 'rgb(255, 0, 0)' Thanks.
In this scenario what's the difference between == and ===?
@Dale == will still work because of JavaScripts loose typing and truthy/falsy (vs. hard booleans) nature. But the suggestion is to get into the habit of using === when you are expecting a specific value of a specific type so that JavaScript can't misinterpret your intent. For THIS answer? Might not matter. But it's a good habit to have regardless.
1

@Sinetheta is right but I was working on it in jsfiddle, so I thought I'd post my work ;p

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.