0

I want to move the tableRow upwards in a html table if the user clicks on it. Here is the html:

<tr id="duration_row" onclick="moveUp('duration_row', 'sort_table')">

I decided to write my own Javascript function, so here is the function I want to use:

function moveUp(row, table){
//  alert(document.getElementById(table).rows[5]);
//  alert(up);
var rows = document.getElementById(table).rows;

for ( var i = 1; i < rows.length; i++) {
    if (row === rows[i]) {

        var tempRow = rows[i - 1];
        rows[i - 1] = rows[i];
        rows[i] = tempRow;
    }
}

}

It is quite straightforward, it iterates over the array of table rows and swaps the matching one with the one above it. Still it does not work, because it compares a string with a object HTMLTableRowElement. So I need to get the id of the HTMLTableRowElement. Any idea how to overcome on this issue? (Maybe the whole approach is wrong, so if you have better idea let me know.)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sanyifejű
  • 2,610
  • 10
  • 46
  • 73
  • http://stackoverflow.com/questions/1569889/jquery-move-table-row This uses jQuery – DDK Jun 19 '13 at 11:24

2 Answers2

0

use jQuery. Methods like after(), insertAfter() [ or before() and insertBefore() ]in jQuery might serve your purpose.

You can use the remove() method to remove the clicked item and then append it just before the element above it (figure out how you can get the reference of the element just above it)

sumitb.mdi
  • 1,010
  • 14
  • 17
0

if you need the content of the row :

 var id =document.getElementById("table-id").rows[i].id;
Muath
  • 4,351
  • 12
  • 42
  • 69