0

How can i only get the values in the serial column if the checkbox is checked for that row using jquery? I currently can get the serial value but not sure how to add the checkbox condition.

Html:

<table id="usersTable">
   <thead>
     <tr>
       <th>Name</th>
       <th>Serial</th>
       <th>Status</th>
       <th>Enabled</th>
    </tr>
  </thead>
  <tbody>
    <tr>
       <td>Name 1</td>
       <td>111111</td>
       <td>Online</td>
       <td><input type="checkbox"></td>
   </tr>
   <tr>
       <td>Name 2</td>
       <td>222222</td>
       <td>Offline</td>
       <td><input type="checkbox"></td>
   </tr>
   <tr>
       <td>Name 3</td>
       <td>333333</td>
       <td>Online</td>
       <td><input type="checkbox"></td>
   </tr>
  </tbody>
</table>

Jquery:

$('#usersTable > tbody  > tr').each(function (index, tr) {
      console.log($(this).find('td:nth-child(2)').text());

 });
3
  • stackoverflow.com/questions/2204250/… Commented Apr 30, 2020 at 5:43
  • You might wanna check this link out Commented Apr 30, 2020 at 5:43
  • console.log( $(this).find('input').is(':checked') ) // return true or false Commented Apr 30, 2020 at 5:47

1 Answer 1

1

There are a million ways to do this, but hopefully this is useful/helpful.

The gist of the solution is to loop over each row, which you did, then only log the text of the second column when the checkbox for that same row has been checked.

$('#usersTable > tbody  > tr').each(function() {
  const currentRow = $(this);
  const lastColumn = currentRow.find('td:last-child')

  if (lastColumn.find('input').attr('checked')) {
    console.log(currentRow.find('td:nth-child(2)').text());
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.