learning
learning

Reputation: 11725

checkbox val not showing false

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

Upvotes: 2

Views: 566

Answers (2)

user669677
user669677

Reputation:

this.checked //true/false - not jquery
$(element).checked //does not work
$(element).attr('checked') //checked/NULL *
$(element).is(':checked') //true/false

*note that <input TYPE=CHECKBOX CHECKED=CHECKED> returns in both checked/unchecked state checked

Upvotes: 1

Quentin
Quentin

Reputation: 943563

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

….attr('checked');

Upvotes: 3

Related Questions