Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get table row above (or before) my current row in jQuery?

Say I have a variable which holds a table row.

How would I get the row right before it using javascript/jquery?

like image 503
Shai UI Avatar asked Nov 01 '11 16:11

Shai UI


5 Answers

This:

var prevRow = row.previousElementSibling;

or

var prevRow = $( row ).prev()[0];

([0] is used to "unwrap the jQuery object", to get the DOM reference to that row. If you want to perform jQuery methods on that row, you can just leave it inside, like $( row ).prev().addClass( '...' ); and in that case you don't need a new variable.)

like image 182
Šime Vidas Avatar answered Nov 08 '22 09:11

Šime Vidas


Assuming that the two rows are siblings (not contained in separate tbody or thead elements):

$curRow = $(this); // or however you're getting the current `tr`.
$curRow.prev('tr');

Should get you the previous row.

like image 32
David Thomas Avatar answered Nov 08 '22 08:11

David Thomas


Well you need to do something like so:

$("#cellID").parent().prev()

like image 1
Sam Avatar answered Nov 08 '22 08:11

Sam


Your input is not sufficient but if i understand correctly here is the code for your requirement..

If your Variable contains table row id  then $('#yourVariableName').prev('tr')[0] will work.
If your Variable contains table row Class  then $('.yourVariableName').prev('tr')[0] will work.
If your Variable contains table row index  then $(' table tr').eq(varValue).prev('tr')[0] will work. 

But please specify what your variable will contain.

like image 1
Exception Avatar answered Nov 08 '22 08:11

Exception


Let's say you want to get the input value from the td in the tr above the current one:

$('#id').prev().children('td').children(':text').val();
like image 1
Alucard Avatar answered Nov 08 '22 09:11

Alucard