Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get the position of a child element

Tags:

html

jquery

i need to find the position of a child element.

i have a table and when a td is clicked, i want the position of the td(0,1 or 2)

<table>
<tr>
 <td>   

 </td>
 <td>   

 </td>
 <td>

 </td>
</tr>
</table>

and a script like this

<script>
$("td").click(function(){
  //how do i get the position of the td?
  alert("column " + columnPosition + "is clicked")
});
</script>
like image 296
MichaelD Avatar asked Mar 02 '09 16:03

MichaelD


1 Answers

<script>
$("td").click(function(){
  //how do i get the position of the td?
  alert("column " + $(this).parent().children().index(this) + " is clicked")
});
</script>

edit: I tested it, and it works

like image 56
Thomas Stock Avatar answered Oct 24 '22 17:10

Thomas Stock