Reputation: 8767
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>
Upvotes: 15
Views: 15742
Reputation: 5655
Just for reference, and this is good
<div>First div</div>
<div>Second div</div>
<div>Third div</div>
<div>Fourth div</div>
<script>
$( "div" ).click(function() {
// `this` is the DOM element that was clicked
var index = $( "div" ).index( this );
$( "span" ).text( "That was div index #" + index );
});
</script>
Upvotes: 0
Reputation: 11266
<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
Upvotes: 35