Dale
Dale

Reputation: 1301

Having trouble with jquery if statement with css property

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.

Upvotes: 2

Views: 300

Answers (2)

Sinetheta
Sinetheta

Reputation: 9449

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)

Upvotes: 4

JesseBuesking
JesseBuesking

Reputation: 6596

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

Upvotes: 1

Related Questions