With the introduction of the prop method, now I need to know the accepted way of unchecking a checkbox. Is it:
$('input').filter(':checkbox').removeAttr('checked');
or
$('input').filter(':checkbox').prop('checked',false);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
jQuery 3
As of jQuery 3,
removeAttrdoes not set the corresponding property tofalseanymore:Changelog
Hence only
.prop('checked',false)is correct way when using this version.Original answer (from 2011):
For attributes which have underlying boolean properties (of which
checkedis one),removeAttrautomatically sets the underlying property tofalse. (Note that this is among the backwards-compatibility "fixes" added in jQuery 1.6.1).So, either will work… but the second example you gave (using
prop) is the more correct of the two. If your goal is to uncheck the checkbox, you really do want to affect the property, not the attribute, and there’s no need to go throughremoveAttrto do that.