views:

482

answers:

2

Hello, I have a table. In this table have select element. How can I find in which table row is select element, from within the select's event handler:

$('#selectElemID').live('change', function(){...});

Thanks

+7  A: 

This should do it, if you want the row number of the current select element (which is what I understand from the question):

$('#selectElemID').live('change', function(){
    alert($(this).closest("tr").prevAll("tr").length + 1);
});

To explain:

$(this).closest("tr")

means select the closest parent tr of this select element.

.prevAll("tr").length + 1

means select all the previous rows, and get me the length of the returned collection. Increment it by one to get the current row number, because we are at total previous rows + 1.

For more information:

karim79
+1 for elegance :-)
henchman
@henchman, thank you sir!
karim79
+1  A: 

also:

$('#selectElemID').live('change', function(){
    alert($(this).closest("tr")[0].rowIndex);
});
BbErSeRkK