Multiple Selection For Checkboxes - shiftMultiSelect.js

File Size: 3.82 KB
Views Total: 2854
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Multiple Selection For Checkboxes - shiftMultiSelect.js

shiftMultiSelect.js is a tiny jQuery plugin that allows the user to select/deselect multiple rows in an HTML table by holding the Shift key and clicking associated checkboxes.

Additionally, it provides a Check All functionality that enables the user to select/deselect all rows in your HTML table with a single click.

How to use it:

1. Add a group of checkboxes to table rows as follows. Note that each checkbox must have a unique ID.

<table class="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">Handle</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row"><input type="checkbox" id="id_chk1" class="checkRow" value="1" /></th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
      <th scope="row"><input type="checkbox" id="id_chk2" class="checkRow" value="2" /></th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
      <th scope="row"><input type="checkbox" id="id_chk3" class="checkRow" value="3" /></th>
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>

2. Create a 'Master' checkbox input to check/uncheck all table rows.

<thead>
  <tr>
    <th scope="col"><input type="checkbox" name='checkboxMaster' /></th>
    <th scope="col">First</th>
    <th scope="col">Last</th>
    <th scope="col">Handle</th>
  </tr>
</thead>

3. Load the main script shiftMultiSelect.js after jQuery.

<script src="/path/to/cdn/jquery.slim.min.js"></script>
<script src="/path/to/shiftMultiSelect.js"></script>

4. Get the checked values.

$(':checkbox').change(function() { 
  console.log(checkValArray) 
});
=> ["1", "2", "3", "4", "5"]

5. Use CSS to highlight tables rows that are checked.

.table-primary, .table-primary>th, .table-primary>td { 
  background-color: #7F8C8D; 
  color: #fff; 
}

This awesome jQuery plugin is developed by ryandougc. For more Advanced Usages, please check the demo page or visit the official website.