Why this works
document.getElementById("myCheckbox").checked = false;
whereas this doesn't with jquery:
$("myCheckbox").attr("checked") = false;
Why this works
document.getElementById("myCheckbox").checked = false;
whereas this doesn't with jquery:
$("myCheckbox").attr("checked") = false;
There are a couple of things wrong with your code. First of all, you select elements in jQuery using selectors, just like in CSS. So you need to turn this
$("myCheckbox")
into this, using the hash (#
) for ID selector
$("#myCheckbox")
Secondly, jQuery doesn't really use properties, but methods for everything. Pass one parameter to get a value, two to set it. In this case, you also want to be using prop
and not attr
. So you would need to do:
$("#myCheckbox").prop("checked", false);
You're missing a #
to indicate that myCheckbox is an id, also you cannot set an attribute value directly like that, pass the value as second parameter:
$("#myCheckbox").attr("checked", false);
Also see the docs: ID Selector (“#id”)
$("#myCheckbox").attr('checked','checked');
and
$("#myCheckbox").removeAttr('checked');
To check the checkbox using jquery
$("#myCheckbox").attr( "checked" , true );
Or uncheck it
$("#myCheckbox").attr( "checked" , false );
It'll work if your checkbox has id set to myCheckbox
i.e. id="myCheckbox"
but if your checkbox has class set to myCheckbox
i.e. class="myCheckbox
" then change your selector to
$('.myCheckbox') // here . used to specify class name.
To check all checkboxes use
$("input:checkbox").attr( "checked" , true ); // or set false to uncheck
or if you want to check or uncheck using name attribute then
$("input:checkbox[name='mychk']").attr( "checked" , true ); // if 'mychk' is name of the checkbox it'll be checked
you want to do this :
$("#myCheckbox").attr("checked", "checked");
dont miss the # aswell.