Reputation: 47955
This is the code :
$('#checkBoxContact1').click(function(e) {
e.preventDefault();
if ($('#chbBox1').attr('checked') == true)
$('#chbBox1').attr('checked', 'false');
else $('#chbBox1').attr('checked', 'true');
});
I call this when I click on a link. But in fact seems that it doesnt works...
maybe I should use :checked?
Upvotes: 0
Views: 958
Reputation: 2908
Try like this:
1. $('#chbBox1').attr('checked', ''); // for false
2. $('#chbBox1').attr('checked', 'checked'); //for true
Upto jQuery-1.5.2 #1 is working.
In jQuery- 1.6 its taking as true.
Upvotes: 0
Reputation: 318518
Use .prop()
and more important, do not quote boolean values.
For boolean attributes the pure existence matters. checked=""
, checked="no"
, checked="false"
all result in the element being checked.
To un-check an element through the attribute you'd have to use .removeAttr('checked')
.
However, you can use the property which is an actual boolean. So .prop('checked', true_or_false)
will do what you want. Actually you could even use .attr('checked', true_of_false)
since jQuery 1.6.1 but using .prop()
instead of .attr()
is a bit faster since it doesn't need to check if it's actually a property you are trying to set instead of an attribute.
Upvotes: 5