Sometimes there is a need to check/uncheck all options (for example
Gmail inbox), then question is how it can be implemented using JQuery?
We can write down a quick jQuery snippet that select/deselect all
checkboxes by clicking on “Select All” header checkbox.
Let’s quickly create the HTML form with one header checkbox and several
other checkboxes.
<ul id="ulContainer">
<li>
<input type="checkbox" id="chkSelectAll" />
Select All</li>
<li>
<input class="chkItem" type="checkbox">
Item 1</li>
<li>
<input class="chkItem" type="checkbox">
Item 2</li>
<li>
<input class="chkItem" type="checkbox">
Item 3</li>
<li>
<input class="chkItem" type="checkbox">
Item 4</li>
<li>
<input class="chkItem" type="checkbox">
Item 5</li>
</ul>
|
In JQuery fire the click event of header checkbox and based on that
select/deselect other checkboxes.
$('#ulContainer').on('click', '#chkSelectAll', function () {
var icChecked = $(this).is(':checked');
$("input.chkItem").each(function () {
$(this).prop("checked", icChecked);
});
});
|
You can also see the working jsfiddle of this example.
Comments
Post a Comment