Identify which nth child of tr a specific td is in jQuery

Viewed 36

With reference to the table example below, how do I, in jQuery, identify the descendant number (below 3) is to the row of the column?

$(".colclass").click(function (e) {
  //get td descendant number to tr, ie 3 in this case
});

<table>
   <tr>
      <td></td>
      <td></td>
      <td class="colclass"></td>
      <td></td>
   </tr>
</table>
1 Answers

You can use .index to get the index of the target as a child to the parent element (i.e., tr in this case):

$(".colclass").click(function (e) {
  console.log($(this).index());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
   <tr>
      <td>1</td>
      <td>2</td>
      <td class="colclass">3</td>
      <td>4</td>
   </tr>
</table>

Related