tags:

views:

2704

answers:

1

Hello All,

I'm an intermediate user in jQuery. I know to find the rowIndex of a table using jQUery, but my scenario is a different one. My table(GridView) consists of 20 columns and each column with different controls like textbox, dropdownlist, image, label. All are server side controls in each row. I bind the gridview with the records from the database. Now when I click on any control or onchange of any textbox, I need to get the rowIndex of that changed column's row. Here is the code I've user:

$("#gv1 tr input[name $= 'txtName']").live('click', function(e){
   alert($(this).closest('td').parent().attr('sectionRowIndex'));
});

But I'm unable to get the rowIndex. If I use any html control inside the gridview, I'm able to get the rowIndex. Is there any way to find out the rowIndex when a server control inside the gridview is clicked?

+1  A: 

Try this:

var rowIndex = $(this)
    .closest('tr') // Get the closest tr parent element
    .prevAll() // Find all sibling elements in front of it
    .length; // Get their count
Darin Dimitrov
But I wanna get the rowIndex of the changed Textbox?
engineerachu
Could you provide the HTML of your page?
Darin Dimitrov
<tr> <td> <span id="gv1_ctl02_lblID">1</span> </td> <td> <input type="text" id="gv1_ctl02_txtName" value="Achu" name="gv1$ctl02$txtName"/> </td> <td> <input type="text" value="Madurai" id="txtLocation"/> </td></tr>When I click on gv1_ctl02_txtName, I won't get the rowIndex, but when I click on "txtLocation", I'm able to get.
engineerachu
In your selector `"#gv1 tr input[name $= 'txtName']"` you take only the inputs whose name ends with `txtName`, so it should work fine for the `gv1$ctl02$txtName` textbox and not for the `txtLocation` textbox.
Darin Dimitrov
No, it alerts rowIndex as '0' for all rows. But for txtLocation, it returns the correct index. txtLocation is an HTML control!
engineerachu
@Darin Dimitrov, Your code doesn't work. Any help?
engineerachu