Graham
Graham

Reputation: 8151

How to find out the child number that "this" refers to in jQuery?

A javascript function has reference to the element that it was called from via the "this" keyword.

In my case the element is a TD

I want to know which number child element that this TD is of the parent TR

How do I do that in jQuery?

I am using version 1.7.1

For example:

<tr>
  <td onclick="hasBeenClicked()"></td>
  <td onclick="hasBeenClicked()"></td>
  <td onclick="hasBeenClicked()"></td>
</tr>

function hasBeenClicked()
{
  var childNumber = $(this)....
}

Upvotes: 0

Views: 44

Answers (1)

Try using .index()

Index starts from 0

fiddle Demo

$(this).index();

or

fiddle Demo

$(this).index('td');

Upvotes: 2

Related Questions