29

Possible Duplicate:
Check checkbox checked property using jQuery

I am having an issue picking up when a checkbox is not checked.

Here is my sample code:

$(".get-the-look ul input").click(function(){
    if("this:checked") {
        var product = $(this).attr('alt');
        $("#bob div").remove('.'+product);
    } else {
        alert('asdasd');
    }
});

But the alert (if the checkbox isn't checked) never fires... The 'else' never kicks in no matter what state the checkbox is in?

I don't see where I am going wrong.

0

2 Answers 2

20

you can't pass this:checked as a string - it won't work as it will be interpreted as a string and always evaluate to true

use $(this).is(':checked') instead

2
2

The string "this:checked" will always evaluate to TRUE. Try this.checked instead, i.e., evaluate the property checked on this:

if(this.checked) { ...
3
  • Hmm, a little late on that one :) Also, make sure that you only try to access .checked on <input type="checkbox />.
    – jensgram
    Commented Jan 26, 2011 at 12:06
  • yeah :) but using native this.checked will do its job (and even faster) - gave jquery answer for consistence with the rest of the example and to show how it should be used properly.
    – Tom Tu
    Commented Jan 26, 2011 at 12:21
  • @Tom Hu Yup! Also, I forgot the reference to Utilizing the awesome power of jQuery to access properties of an element which I link to whenever I can :)
    – jensgram
    Commented Jan 26, 2011 at 12:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.