Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: find next table-row

I have a table, containing rows, contaning cells - and some of them contain an img as follows:

<img id="FormView1_btnSave_row1%1%new" style="border-width: 0px; cursor: pointer;"
src="grafik/ok_16x16.gif" onclick="cleverStuff(this)"/>

In the function cleverStuff I would like to operate on the line that follows the line in which the button was clicked - this special button is contained in the last visible line only, there are a bunch of hidden lines below and I want to make the first hidden line visible - but I fail at getting to the next line. My understanding was that all combination of parent() and next() could be used to get from the img to the td, to the tr and finally to the next tr. So I tried to verify this:

$(ctrl).attr('id') correctly returns the img's id :)

$(ctrl).parent().attr('id') returns NULL.

What am I missing here?

like image 256
MBaas Avatar asked Nov 02 '09 15:11

MBaas


4 Answers

This should give you the enclosing tr of the element, even if it isn't the element's direct parent, then that row's next row. if you use parent on an element that is inside a td, it will give you the column, not the row. Supplying a filter to the parent() method will simply filter out the parent unless it happens to match the filter, typically resulting in no matching elements. Closest is probably what you want, but parents('tr') might be needed if you have nested tables and want the outer row instead of the inner row.

$(this).closest('tr').next('tr')
like image 125
tvanfosson Avatar answered Nov 12 '22 22:11

tvanfosson


You don't show us enough HTML really but this should work:

$(ctrl).parent('tr').next();
like image 43
Greg Avatar answered Nov 12 '22 23:11

Greg


Maybe the parent element didn't have its id set? To get to the next row from the image I believe you can do:

$(this).parent('tr').next('tr')
like image 5
airportyh Avatar answered Nov 12 '22 23:11

airportyh


I hope u can use

jQuery(this).parents('tr'); 
like image 1
Roshan Chaudhary Avatar answered Nov 12 '22 22:11

Roshan Chaudhary