Bineesh Bineesh - 4 months ago 7
jQuery Question

How to get value in a td by clicking its button

Iam trying to get the value in a td while clicking a button in it.
I used the code below and get only blank value.

So i tried to change the css to see whether it selectes the specific td. Then

$(this).parents('tr td:nth-child(8)').css('color','red');

didnt work but

$(this).parents('tr').css('color','red');


worked and made that row data in all td in red color. why this ?

Answer

The issue with the first code example is that the td:nth-child(8) is not the parent of the element referenced in this, the tr is.

To solve this you need to use separate calls to parents() and find(), like this:

$(this).parents('tr').find('td:nth-child(8)').css('color','red');

Also note that assuming that you only want to find the immediate tr parent element, you can replace parents() with closest() for slightly improved performance.

Comments