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.)