How do I check/uncheck a checkbox input or radio button?
jQuery Interview Questions and Answers
(Continued from previous question...)
How do I check/uncheck a checkbox input or radio button?
There are two ways to check/uncheck a checkbox/radio button.
Set the 'checked' attribute to true or false.
// Check #x
$('#x').attr('checked', true);
// Uncheck #x
$('#x').attr('checked', false);
Add or remove the 'checked' attribute:
// Check #x
$("#x").attr('checked', 'checked');
// Uncheck #x
$("#x").removeAttr('checked');
You can try an example of checking/unchecking with the following demo:
I'll be checked/unchecked.
and here's the source code to the demo:
<label><input type="checkbox" id="c"/> I'll be checked/unchecked.</label>
<input type="button" value="Check" onclick='$("#c").attr("checked","checked")'/>
<input type="button" value="Uncheck" onclick='$("#c").removeAttr("checked")'/>
(Continued on next question...)
Other Interview Questions
|