I have a table where I need the first two cells of each row that can be clicked (NOT the entire row). When I click the first or seccond cell, I want to get the value of the third cell of the same row. To clarify when I press a1 , I want the warning to show c1 . If I press b2 , I want it to show c2 , and if I press c3 , I do not want something to happen.
As you can see, my alert($(this).parent(':nth-child(3)').text()); does not work. How can i achieve this?
$('td:nth-child(-n+2)').on("click", function(){ alert($(this).parent(':nth-child(3)').text());
td{ border: 1px solid black; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <table> <tr> <td>a1</td> <td>b1</td> <td>c1</td> </tr> <tr> <td>a2</td> <td>b2</td> <td>c2</td> </tr> <tr> <td>a3</td> <td>b3</td> <td>c3</td> </tr> </table>
source share