Kaur Kaur - 3 months ago 15
jQuery Question

How to get table column values using Div Id selector

I have two questions. Table id is not available in the DOM. How do I use Div id in the following case to get to 3rd row column 2 value of '10'?

I get error on the following code which is iterating through second columns. How do I get only third row, second column?

$("'#part_li3_tt table'").children("tbody").children("tr").children("td:nth-child(2)").each(function () {
alert("third row second col Value: " + $(this).html());
});

<div id="part_li3_tt">
<table>
<tbody>
<tr>
<td>FName:</td>
<td>Sara</td>
</tr>
<tr>
<td>LName:</td>
<td>Rodriguez</td>
</tr>
<tr>
<td>Class:</td>
<td>10</td>
</tr>

</tbody>
</table>
</div>

Answer

You should be able to do something like this:

alert($('#part_li3_tt table tr:nth-of-type(3) td:nth-of-type(2)').html());

However, I'm not sure why you need the id part_li3_tt in the first place, so you could probably just do this instead:

alert($('table tr:nth-of-type(3) td:nth-of-type(2)').html());

If you're confused about how the nth-of-type jQuery selector works, you can read more about it here: https://api.jquery.com/nth-of-type-selector/