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
data. find(selector); Then to find the value of different cells in a row, we'll have to specify the cell which will be done using the eq() method in jQuery. This method returns the value with a specific index number of the selected elements.
jQuery Misc index() Method The index() method returns the index position of specified elements relative to other specified elements. The elements can be specified by jQuery selectors, or a DOM element. Note: If the element is not found, index() will return -1.
To count the number of rows, the “#Table_Id tr” selector is used. It selects all the <tr> elements in the table. This includes the row that contains the heading of the table. The length property is used on the selected elements to get the number of rows.
EDIT (two years later): Please don't do it the way I previously described, it is a total waste as table rows already have a rowIndex
property, so there's just no need to compute anything:
$('#selectElemID').live("change", function (){
alert($(this).closest("tr")[0].rowIndex);
});
Demo.
<silliness>
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:
</silliness>
also:
$('#selectElemID').live('change', function(){
alert($(this).closest("tr")[0].rowIndex);
});
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