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

Let's say I have a variable that contains a table row.

How do I get a line directly in front of it using javascript / jquery?

+5
source share
5 answers

It:

var prevRow = row.previousElementSibling;

or

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

( [0]Used to "expand the jQuery object" to get the DOM link for this line. If you want to execute jQuery methods on this line, you can just leave it inside, for example $( row ).prev().addClass( '...' );, in this case you do not need a new variable.)

+14
source

, ( tbody thead):

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

.

+2

, - :

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

+1

Your input is not enough, 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 indicate what your variable will contain.

+1
source

Suppose you want to get an input value from td to tr above the current:

$('#id').prev().children('td').children(':text').val();
+1
source

All Articles