prepare123 prepare123 - 7 months ago 13
Javascript Question

how to find table tag td values useing jQuery filter selecter

<table>
<tr>
<td>Calories </td>
<td>TargetValue1</td>
</tr>
<tr>
<td>Protein</td>
<td>TargetValue2</td>
</tr>
<tr>
<td>Protein</td>
<td>TargetValue3</td>
</tr>




hi how can I select all second td values??

i tried

$.each($("#nutritab tbody tr td:eq(1)"),function(i , item){
alert($(item).text());
});


but that was return only first values ....

Answer

Use nth-child selector instead of :eq(index)

The :nth-child(n) selector matches every element that is the nth child()(Index starts from 1)

The :eq(index) selector selects the element at index n within the matched set.(Zero-based index)

$.each($("#nutritab tr td:nth-child(2)"), function(i, item) {
  alert($(item).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id='nutritab'>
  <tr>
    <td>Calories</td>
    <td>TargetValue1</td>
  </tr>
  <tr>
    <td>Protein</td>
    <td>TargetValue2</td>
  </tr>
  <tr>
    <td>Protein</td>
    <td>TargetValue3</td>
  </tr>
</table>

Comments