Bineesh Bineesh - 1 year ago 63
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


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

Answer Source

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:


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download