Fought with a bunch of examples and, being still new to jQuery/Javascript, cannot get my code to function (here my my template in gsp):
<table> <thead> <tr> <th><input type="checkbox" name="selectAll" onclick="selectAll(this.checked);"/></th> </tr> </thead> <tbody> <td> <td><input type="checkbox" name="domainList" value="${domainInstance.id}"/></td> </tbody> <table>
I have the following javascript snippet in my main gsp, that calls the template:
function selectAll(status) { }
How do I select all checkboxes from the selectAll name?
Abstract: To Select or Deselect Checkboxes using jQuery, all you need to do is use the prop() method along with the change event to achieve the requirement in a couple of lines of code.
In order to select all the checkboxes of a page, we need to create a selectAll () function through which we can select all the checkboxes together. In this section, not only we will learn to select all checkboxes, but we will also create another function that will deselect all the checked checkboxes.
Toggle Checkbox by Simulating a Mouse ClickBy using the click() method on the checkbox we can simulate a mouse click, which would flip the checkbox value. For example: checkbox. click();
click(function() { $("input[name=recipients\\[\\]]"). attr('checked', true); }); });
Since you are using jQuery, you should use an onclick
handler like below for selectAll.
$(':checkbox[name=selectAll]').click (function () { $(':checkbox[name=domainList]').prop('checked', this.checked); });
Please note that the above code is going to look into the entire dom for the checkbox with name=selectAll
and set the status of the checkbox with name=domainList
.
Below is a slightly better version with minor markup change,
jsFiddle DEMO
$('#selectAllDomainList').click(function() { var checkedStatus = this.checked; $('#domainTable tbody tr').find('td:first :checkbox').each(function() { $(this).prop('checked', checkedStatus); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <table id="domainTable"> <!-- Added ID --> <thead> <tr> <th> <!-- Added ID to below select box --> <input type="checkbox" name="selectAll" id="selectAllDomainList" /> </th> </tr> </thead> <tbody> <tr> <td> <input type="checkbox" name="domainList" value="${domainInstance.id}" /> </td> </tr> <tr> <td> <input type="checkbox" name="domainList" value="${domainInstance.id}" /> </td> </tr> <tr> <td> <input type="checkbox" name="domainList" value="${domainInstance.id}" /> </td> </tr> <tr> <td> <input type="checkbox" name="domainList" value="${domainInstance.id}" /> </td> </tr> </tbody> <table>
$("input:checkbox").prop("checked", status);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With