jquery beginner: change background on click

Posted by user1873217 on Stack Overflow See other posts from Stack Overflow or by user1873217
Published on 2012-12-03T16:52:54Z Indexed on 2012/12/03 17:03 UTC
Read the original article Hit count: 140

Filed under:
|
|

I'm trying to change the background of an element on click based on the current color.

Here's the relevant html:

<div id="rect" style="height: 100px; width:300px; background: red">
</div>

and my jquery attempt:

    $("#rect").click(function() {
    if ($(this).css('background') == 'red') {
        $(this).css('background','blue');}
    else {$(this).css('background','yellow');}
    });

I'm always getting a yellow box; the 'true' condition never fires.

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about colors