Issue on Getting Selected Row Values From Table with Checkbox

Issue on Getting Selected Row Values From Table with Checkbox


Can you please take a look at above demo and let me know why I am not able to get the values of selected (checked) rows from the table? I have a code like this:

  1. $("#btn").on("click", function () {
  2.     var checkedRows = [];
  3.     $("#tbody tr").each(function () {

  4.         if ($(this).find("input").is(":checked")) {
  5.             checkedRows.push($(this).find("td:eq(1)").html());
  6.         }
  7.         // console.log(checkedRows);    
  8.     });

  9.         var result = [];

  10.     $.each($("td:eq()"), function (idx, item) {
  11.         if (checkedRows.indexOf(idx> -1)){ result.push(item);}
  12.     });

  13.     if (result.length < 2) {
  14.         alert("Please Select 2 to 4 Models");
  15.     }
  16.     if (result.length > 4) {
  17.         alert("Please Select 2 to 4 Models");
  18.     } else {
  19.         console.log(result);
  20.     }
  21.     });
and HTML as:

  1. <table border="1" width="100%">
  2.         <thead>
  3.                 <tr>
  4.                     <td><input type='checkbox' /></td>
  5.                         <td>Row 1, cell 2</td>
  6.                         <td>Row 2, cell 3</td>
  7.                 </tr>
  8.         </thead>
  9.         <tbody>
  10.                 <tr>
  11.                         <td><input type='checkbox' /></td>
  12.                         <td>Row 2, cell 2</td>
  13.                         <td>Row 2, cell 3</td>
  14.                 </tr>
  15.                 <tr>
  16.                         <td><input type='checkbox' /></td>
  17.                         <td>Row 3, cell 2</td>
  18.                         <td>Row 3, cell 3</td>
  19.                 </tr>
  20.                 <tr>
  21.                         <td><input type='checkbox' /></td>
  22.                         <td>Row 4, cell 2</td>
  23.                         <td>Row 4, cell 3</td>
  24.                      <tr>
  25.                         <td><input type='checkbox' /></td>
  26.                         <td>Row 4, cell 2</td>
  27.                         <td>Row 4, cell 3</td>
  28.                 </tr>
  29.                 </tr>
  30.         </tbody>
  31. </table>
  32. <input id="btn" type="button" label="button" value="get rows" />

Thanks,