0

I have a check box on my form with id="chkBold". When i click or check this checkbox, i want element

to change color. Here is my code and entire page. It does not change color. Can you see what is wrong here?

Thanks.

(function () {
    $("#chkBold").click(function(){
        if($("#chkBold").is(':checked')) {
            $("p").css('color', 'Blue'); 
        }else{
            $("p").css('color', 'Red'); 
        }
     });
});


 jsfiddle: http://jsfiddle.net/netten/dAesw/
8
  • add the html part please Commented Jul 23, 2012 at 6:46
  • shareef, i tried t oadd html part but it is giving formatting issues. Commented Jul 23, 2012 at 6:49
  • it's here = JSFIDDLE Commented Jul 23, 2012 at 6:50
  • Here is my html code :jsfiddle.net/netten/dAesw Commented Jul 23, 2012 at 6:58
  • And it's working if you remove all the strangeness - FIDDLE Commented Jul 23, 2012 at 7:04

4 Answers 4

3

You're missing a dollar sign :

$(function () {
    $("#chkBold").click(function(){
        if($(this).is(':checked')) {
            $("p").css('color', 'blue'); 
        }else{
            $("p").css('color', 'red'); 
        }
     });
});
Sign up to request clarification or add additional context in comments.

2 Comments

@netten - did you add jQuery ?
Yes, because some other functions like button clicks etc worked.
0

Check this working model ---

http://jsfiddle.net/swapnesh/Xwj6n/

Comments

0

Try to debug at

if($("#chkBold").is(':checked'))
               {
                alert($('p'));   
                $("p").css('color', 'Blue'); 
                }

make sure this selector gives you the same element you want to change the color, one more thing , Do you want to change the font color or the backgroundColor, both are different.

2 Comments

Rupesh, it does not matter whether background or font color. All i am trying is to see that color changes on a checkbox click.
@netten ok it was just a point, I just wanted to ensure. One thing i can tell is first check the selectors you used like $("#chkBold").is(':checked'). place alerts at different locations and debug your self It will be more helpful to you in future also. Regards :)
0

I just tried tidying up your jsfiddle and it seems to work... http://jsfiddle.net/8fggu/2/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.