Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - toggle select all checkboxes

Tags:

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?

like image 746
user82302124 Avatar asked Mar 12 '12 14:03

user82302124


People also ask

How do I check uncheck all checkboxes with a button using jQuery?

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.

How do I select all checkboxes?

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.

How do I toggle checkbox value?

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();

How check toggle is checked or not in jQuery?

click(function() { $("input[name=recipients\\[\\]]"). attr('checked', true); }); });


2 Answers

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>
like image 73
Selvakumar Arumugam Avatar answered Oct 03 '22 16:10

Selvakumar Arumugam


$("input:checkbox").prop("checked", status); 
like image 22
Ray Avatar answered Oct 03 '22 17:10

Ray