The checkbox can be easily be checked or unchecked using jQuery with just a line of code. Suppose there is a checkbox in the web page, and you have to check or uncheck it using jQuery. Here you have to perform the given steps.
The checkbox is:
<input type="checkbox" id="myCheckbox" class="myCheckboxCss"/>
Use the function prop() to check the checkbox using its id.
$('#myCheckbox').prop('checked', true);
Check the Checkbox using its class.
$('.myCheckboxCss').prop('checked', true);
In the same way uncheck this checkbox.
$('#myCheckbox').prop('checked', false);
Uncheck the Checkbox using its class.
$('.myCheckboxCss').prop('checked', false);
For jQuery 1.6+ the attr() function will no longer work for Checking and Unchecking Checkboxes, therefore use prop() in place of attr() in such situations.
Sometimes you want to add a feature in our web page where all checkboxes inside an asp.net checkboxlist control gets checked & unchecked from a main checkbox. This can be easily done with some jQuery Code.
Here you will use a main checkbox control for doing this task, then place a click event for this checkbox.
In this click event loop against all input controls of type checkbox which is placed inside checkboxlist control, and check or uncheck them depending upon whether the main checkbox is checked or not.
<asp:CheckBox ID="checkBox" Text="Buy All" Font-Bold="true" runat="server" />
<asp:CheckBoxList ID="checkBoxList" TextAlign="Right" runat="server">
<asp:ListItem Enabled="true" Value="Driver_Steering">Steering</asp:ListItem>
<asp:ListItem Value="Driver_Body">Body</asp:ListItem>
<asp:ListItem Value="Driver_Reflectors">Reflectors</asp:ListItem>
<asp:ListItem Value="Driver_Horn">Horn</asp:ListItem>
<asp:ListItem Value="Driver_Leaks">Leaks</asp:ListItem>
<asp:ListItem Value="Driver_Ammeter">Ammeter</asp:ListItem>
<asp:ListItem Value="Driver_Transmission">Transmission</asp:ListItem>
<asp:ListItem Value="Driver_Tail_Lights">Tail Lights</asp:ListItem>
<asp:ListItem Value="Driver_Coupling_Device">Coupling Device</asp:ListItem>
<asp:ListItem Value="Driver_Service_Brakes">Service Brakes</asp:ListItem>
<asp:ListItem Value="Driver_Glass">Glass</asp:ListItem>
<asp:ListItem Value="Driver_Exhaust">Exhaust</asp:ListItem>
<asp:ListItem Value="Driver_Other_Items">Other Items</asp:ListItem>
<asp:ListItem Value="Driver_Drive_Line">Drive Drive Line</asp:ListItem>
<asp:ListItem Value="Driver_Clutch">Clutch</asp:ListItem>
<asp:ListItem Value="Driver_Speedometer">Speedometer</asp:ListItem>
</asp:CheckBoxList>
The jQuery Code is:
<script type="text/javascript">
$(document).ready(function () {
$('#checkBox').click(function () {
if ($(this).is(":checked")) {
$('[id *= checkBoxList]').find('input[type="checkbox"]').each(function () {
$(this).prop("checked", true);
});
}
else {
$('[id *= checkBoxList]').find('input[type="checkbox"]').each(function () {
$(this).prop("checked", false);
});
}
});
});
</script>
Note – I have used [id *= checkBoxList] to find an elemeent whose id contains checkBoxList, and so it gives the checkboxlist element.
This code is provided by someone in the comments section. I have tested it and it works great.
$("#checkBox").change(function () {
$("#checkBoxList input").prop('checked', $(this).prop("checked"));
});
Let me explain how it works –
Note: Checkbox are input element of type=”checkbox”.
Download the codes: