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?

3
  • can you add html part of that table,, with form and checkboxes? Commented Dec 11, 2017 at 7:42
  • 2
    did you even tried searching already? here it is stackoverflow.com/questions/11292778/… Commented Dec 11, 2017 at 7:43
  • You have given code in jquery but the tag is javascript Commented Dec 11, 2017 at 7:49

1 Answer 1

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">

Sign up to request clarification or add additional context in comments.

2 Comments

This is jquery, the tag is javascript
Yah! your correct @Omkaar.k but inside of the code in above question is using jquery right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.