0

In my project, I want to instor checkbox value to array.

Here is my code:

$('#fpdId').click(function(){
    var files = new Array();

    //xzyId is table id.
    $('#xzyId tbody tr  input:checkbox').each(function() {
      if (this.checked) {
      files.push(this.value);
      }
    });

    console.log(files);
 });

  <input type="button" id="fpdId" value="filePack">

My html table has three rows and each row has three tds

Here is table code:

 <table border=1px class="xzy" id="xzyId" style="width:100%">
        <colgroup>
          <col style="width:10%">
          <col style="width:80%">
          <col style="width:10%">
        </colgroup>
       <tbody>
        <tr> 
           <td ><input type="checkbox" value="x" >1</td>
           <td >Stack</td>
           <td >John</td>
        </tr>
        <tr> 
           <td ><input type="checkbox" value="y" >2</td>
           <td >Stack</td>
           <td >Sansa</td>
        </tr>
        <tr> 
           <td ><input type="checkbox" value="z" >3</td>
           <td >Stack</td>
           <td >Aya</td>
        </tr>
       </tbody>
      </table>

But unlucky, the array is empty, What is wrong?

Snow
  • 71
  • 1
  • 11

1 Answers1

2

It will be helpful to you

I have changed.

  if ($(this).is(':checked')) {
  }

$("document").ready(function(){
$('#fpdId').click(function(){
    var files = new Array();

    //xzyId is table id.
    $('#xzyId tbody tr  input:checkbox').each(function() {
      if ($(this).is(':checked')) {
      files.push(this.value);
      }
    });

    console.log(files);
 });

})
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border=1px class="xzy" id="xzyId" style="width:100%">
    <colgroup>
      <col style="width:10%">
      <col style="width:80%">
      <col style="width:10%">
    </colgroup>
   <tbody>
    <tr> 
       <td ><input type="checkbox" value="x" >1</td>
       <td >Stack</td>
       <td >John</td>
    </tr>
    <tr> 
       <td ><input type="checkbox" value="y" >2</td>
       <td >Stack</td>
       <td >Sansa</td>
    </tr>
    <tr> 
       <td ><input type="checkbox" value="z" >3</td>
       <td >Stack</td>
       <td >Aya</td>
    </tr>
   </tbody>
  </table>

 <input type="button" id="fpdId" value="filePack">
Kalaiselvan
  • 2,095
  • 1
  • 18
  • 31