skip to Main Content

I have table that have checkbox and button out side the table that will toggle from select to unselect , how do I click anywhere on the table row and my checkbox tick regarding where I will be selected one by one and when it reaches the last table row my button change from select to unselect, I did manage to click the table row and checkbox tick , what I’m failing is when I reach the last table row and button change from select to unselect.

I have tried to get the length of row

$('tr').click(function(event) {
  var $target = $(event.target);
  if (!$target.is('input:checkbox')) {
    var select_chk = document.getElementById("button1");
    $(this).find('input:checkbox').each(function() {
      if ((this.checked)) {
        this.checked = false;
        button.value = "Unselect"
      } else {
        this.checked = true;
        button.value = "Select"
      }
    })
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" id="check" value="Select" />
<table>
  <thead>
    <tr>
      <th></th>
      <th>Name</th>
      <th>Surname</th>
      <th>Gender</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input type="checkbox" /></td>
      <td>Cliff</td>
      <td>Deon</td>
      <td>Male</td>
      <td>52</td>
    </tr>
    <tr>
      <td><input type="checkbox" /></td>
      <td>Dennis</td>
      <td>Van Zyl</td>
      <td>Male</td>
      <td>25</td>
    </tr>
  </tbody>
</table>

2

Answers


  1. There were several issues.

    Here is a working version that also handles clicking the button

    I count the number of checked checkboxes are equal to total number of checkboxes.

    I added an ID to the tbody

    Also I use ternaries

    const $select_chk = $("#check");
    const $table = $("#tbl")
    const $checks = $table.find('input:checkbox')
    const checkLength = $checks.length;
    $table.on("click","tr", function(e) {
      if (!e.target.matches("[type=checkbox]")) { // if not a checkbox
        $(this).find("input:checkbox").click()    // click it anyway
      }  
      $select_chk.val(checkLength === $table.find('input:checkbox:checked').length ? "Unselect" : "Select");
    });
    $select_chk.on("click", function() { // we need function to get "this"
      const chk = this.value === "Select"; // check if text is select
      $checks.each(function() {
        this.checked = chk;
      });
      $select_chk.val(chk ? "Unselect" : "Select"); // toggle the text
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="button" id="check" value="Select" />
    <table>
      <thead>
        <tr>
          <th></th>
          <th>Name</th>
          <th>Surname</th>
          <th>Gender</th>
          <th>Age</th>
        </tr>
      </thead>
      <tbody id="tbl">
        <tr>
          <td><input type="checkbox" /></td>
          <td>Cliff</td>
          <td>Deon</td>
          <td>Male</td>
          <td>52</td>
        </tr>
        <tr>
          <td><input type="checkbox" /></td>
          <td>Dennis</td>
          <td>Van Zyl</td>
          <td>Male</td>
          <td>25</td>
        </tr>
      </tbody>
    </table>
    Login or Signup to reply.
  2. //button click
    $('#check').click(function(e) {
        if($(this).val()=='Select'){
            $('td input:checkbox').prop('checked',true);
            $(this).val('Unselect');
        }else{
             $('td input:checkbox').prop('checked',false);
             $(this).val('Select');
        }
    });
    
    //checkbox change
    var $checkboxes = $('td input[type="checkbox"]');      
    $checkboxes.change(function(){
        var countCheckedCheckboxes = $checkboxes.filter(':checked').length;
        if(countCheckedCheckboxes==$('td input[type="checkbox"]').length)
        {
            $('#check').val('Unselect');
        }
        else{
             $('#check').val('Select');
        }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="button" id="check" value="Select" />
    <table>
      <thead>
        <tr>
          <th></th>
          <th>Name</th>
          <th>Surname</th>
          <th>Gender</th>
          <th>Age</th>
        </tr>
      </thead>
      <tbody id="tbl">
        <tr>
          <td><input type="checkbox" /></td>
          <td>Cliff</td>
          <td>Deon</td>
          <td>Male</td>
          <td>52</td>
        </tr>
        <tr>
          <td><input type="checkbox" /></td>
          <td>Dennis</td>
          <td>Van Zyl</td>
          <td>Male</td>
          <td>25</td>
        </tr>
      </tbody>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search