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

3
  • Can you provide your code with snippet. Commented Oct 24, 2016 at 9:28
  • I'm really sorry.. I'm posting this using mobile Commented Oct 24, 2016 at 9:29
  • Possible duplicate of Get second td of tr using jquery Commented Oct 24, 2016 at 10:01

4 Answers 4

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>

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

Comments

1

Try this.

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

1 Comment

please quote properly your 'td:eq(1)"
0

First you have to get the parent.

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

Comments

0

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

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

Comments

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.