vendredi 29 avril 2016

Getting data from a specific column in a HTML table when a checkbox in that row is checked using Javascript/JQuery

I have a HTML table with 5 columns. The first column is a checkbox. I want to find the content of the 5th column (last column) when the checkbox in that row is checked.

HTML :

    <table>
     <tr>
      <th></th>
      <th>A</tr>
      <th>B</tr>
      <th>C</tr>
      <th>D</tr>
     </tr>
     <tr>
      <td><input type='checkbox' class="chk" /></td>
      <td>data for A1</td>
      <td>data for B1</td>
      <td>data for C1</td>
      <td>data for D1</td>
     </tr>
     <tr>
      <td><input type='checkbox' class="chk"/></td>
      <td>data for A2</td>
      <td>data for B2</td>
      <td>data for C2</td>
      <td>data for D2</td>
     </tr>
    </table>

I am a beginner in javascript but have tried doing this.

**JS : **

    $(document).ready(function(){
      if($('input.chk').is(':checked')){
         var y = $("td:nth-of-type(5)").html();
         alert(y);
         }
      });

This function returns only for the first row i.e when the checkbox is checked it shows "data for D1". But I want it for all the rows.

Thanks for your help. :)




Aucun commentaire:

Enregistrer un commentaire