Home > Blockchain >  jQuery: count input checked in a table
jQuery: count input checked in a table

Time:10-19

I'm trying to get the number of machines I selected.

here we have a list of virtual machines and I would like to count the selection that is when I press an input there is the element that is checked.

i tried this but not working.

   $("table input[type=checkbox]").click(function(){
        let nbchecked = $("table input[type='checkbox']:checked").length;
        console.log(nbchecked);
        let len = nbchecked.length;

          $(".counter").html(nbchecked); //inject the number of vm checked in the div
    });

I am a beginner in javascript and jquery and I don't know how to do it. I need your help plz

CodePudding user response:

It work, post your html plz

$("table input[type=checkbox]").click(function(){
      let nbchecked = $("table input[type='checkbox']:checked").length;
      console.log(nbchecked);
      let len = nbchecked.length;

        $(".counter").html(nbchecked); //inject the number of vm checked in the div
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table" id="vm_table">
  <tbody>
    <tr>
      <td>
        <label for="id_vms_1">
          <input type="checkbox" name="vms" value="12" id="id_vms_1"> vm1
        </label>
      </td>
    </tr>
    <tr>
      <td>
        <label for="id_vms_2">
          <input type="checkbox" name="vms" value="13" id="id_vms_2"> vm2
        </label>
      </td>
    </tr>
    <tr>
      <td>
        <label for="id_vms_3">
          <input type="checkbox" name="vms" value="14" id="id_vms_3"> vm3
        </label>
      </td>
    </tr>
  </tbody>
</table>
<div class="counter"></div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related