2

There will be lot of rows in the table and delete buttons for each row.. while clicking delete button, I'm trying to fetch two td values.. and it's returning null

Html is

<tr>
   <td> abc</td>
     <td> def</td>
       <td> <input type="button" class=" delete"  value=" delete"></td>

Jquery code on clicking button

  jQuery(this).find("tr td:eq(1)").text();

Is there anything wrong? It should get the second td value of which ever row when we click delete button

Syed
  • 2,471
  • 10
  • 49
  • 89

4 Answers4

1

You need to go back to the parent first, then find the child.

$('.delete').on('click', function(){
  txt = $(this).parents('tr').find("td:eq(1)").text();
  console.log(txt);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>abc</td>
    <td>def</td>
    <td>
      <input type="button" class="delete" value="delete">
    </td>
    <tr>
</table>
Miro
  • 8,402
  • 3
  • 34
  • 72
1

Try this.

jQuery(this).closest('tr').find('td:eq(1)').text();
Vicky_Burnwal
  • 951
  • 9
  • 14
0

First you have to get the parent.

jQuery(this).parents('tr').find("td:eq(1)").text();
Huelfe
  • 1,776
  • 16
  • 25
0

use closest() function and get its parent tr and find "td:eq(1)"

jQuery(this).closest('tr').find('td:eq(1)').text();
Beginner
  • 4,118
  • 3
  • 17
  • 26