When i check\uncheck checkboxes and radiobuttons, browser doesnt set attribute checked. So, when i am trying uncheck checkbox using jquery:
$('#tab2 input').removeAttr('checked');
it doesnt work. It works case checkbox was checked from my javascript code. How can i uncheck checkbox which already checked by user?
When i check\uncheck checkboxes and radiobuttons, browser doesnt set attribute checked. So, when i am trying uncheck checkbox using jquery:
$('#tab2 input').removeAttr('checked');
it doesnt work. It works case checkbox was checked from my javascript code. How can i uncheck checkbox which already checked by user?
Share asked May 13, 2011 at 15:00 Neir0Neir0 13.4k28 gold badges90 silver badges143 bronze badges 2- 4 Since the API for this stuff changed recently, you should specify whether you're using jQuery 1.5.x or under, or jQuery 1.6 or above. – Jamie Wong Commented May 13, 2011 at 15:04
- @Jamie Wong i'm using jQuery 1.6 – Neir0 Commented May 13, 2011 at 15:12
3 Answers
Reset to default 5You're setting a property, so use .prop()
in jQuery 1.6
.
$('#tab2 input').prop('checked',false);
Or if there's only one input
, just change the property of the element directly.
$('#tab2 input')[0].checked = false;
When Or upgrade to jQuery 1.6.1
is released,jQuery 1.6.1
and you'll be able to go back to using .attr()
to set properties again, but .prop()
will still be the better way to do it.
From the release notes:
Specifically, boolean attributes such as checked, selected, readonly, and disabled in 1.6.1 will be treated just as they used to be treated in jQuery versions prior to 1.6. This means that code such as
$(“:checkbox”).attr(“checked”, true); $(“option”).attr(“selected”, true); $(“input”).attr(“readonly”, true); $(“input”).attr(“disabled”, true);
or even:
if ( $(“:checkbox”).attr(“checked”) ) { /* Do something */ }
will not need to be changed in 1.6.1 in order to work as previously expected.
$('#tab2 input').attr('checked',false);
$('input[name=foo]').attr('checked', false);