Afternoon,
say I have a table like so
<table id="foo">
<tr><td><!-- label --><!-- textbox --></td><tr>
<tr><td><!-- label --><!-- textbox --></td><tr>
<tr><td><!-- label is bar --><!-- textbox --></td><tr>
<tr><td><!-- label --><!-- textbox --></td><tr>
<tr><td><!-- label --><!-- textbox --></td><tr>
</table>
how could I in JQuery determine the index of the row that has had it’s textbox amended (on blur)? So if a user adds a value to the textbox on the third row (“label is bar”) I could alert the row index (in this case 2)?
I’m assuming I’d get the table id, perform an each on the tr, then find the text box using children and when that child has it’s onblur event activated we then use parent to output the index?
Any ideas?
Thanks
This attaches the
blurevent to allinputelements withtype="text". You might want to narrow down that selector to only select inputs inside your table.In the event handler function, it gets the closest
tr(that is, the first ancestor element ofthiswhich is atr, and gets the index of that).Here’s an example fiddle showing it in action.
As noted in another answer, you may actually be looking for the
changeevent, rather than theblurevent as you say in your question. Thechangeevent will fire when the value of the input actually changes, as opposed to whenever it loses focus. If that’s the case, simply change the wordblurtochange.