I've a table that contains 3 columns. I need to bind an event that fires off whenever one of those columns is clicked using jQuery.
However, I need to know the index of the column clicked.
i.e: First column (index 0), Second column (index 1), Third column (index 2), and so on...
How can I do that?
var firstRow:
var firstRow = $("tr:first > th", "table[id*=Grid]");
Take a look:
firstrow.click(function(e){
//var id = e.target.index;
var id = $(e).parent().children().index(this);//returns -1
})
You can do this using .index() (it's 0 based), like this:
$("td").click(function() {
var i = $(this).parent().children().index(this);
alert(i);
});
It might be a better idea to use native javascripts .rowIndex instead of
jQuerys .index. jQuery might have some trouble in detecting table head (TH) elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With