views:

27

answers:

1

I have the follwoing code:

 $("input:checkbox").live('click', function() {
                          alert($(this).val());
});

True is shown if the checkbox is checked. However, if the checkbox is checked and I uncheck it, the value shown is still true. How can I correct that? What is wrong with the code? Thanks

+3  A: 

The value of a checkbox is always the same. You need to see if it is checked or not.

….attr('checked');
David Dorward
+1 - Also there's `this.checked` (fastest) or `.is(':checked')` :)
Nick Craver
Oh no, I've fallen into the "They are writing it in jQuery. Must use jQuery no matter how inefficient it is" trap!
David Dorward