Somehow I just get stuck getting this to work. I have a table in which I exchange rows with two buttons for up and down. This works, but I have to exclude 1 column from that operation. What is going wrong here? As I have the selectors now, nothing is coming up, neither for the row which is moved nor the row which makes place, both up and down. What is the best way to shape the selectors or is there any other way to make this happen?At this moment a have this code:
<!DOCTYPE html>
<html>
<head>
<title>energiesale test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="scripts/jquery-1.11.1.min.js"></script>
</head>
<body>
<table>
<thead><tr>
<th>header1</th><th>header2</th><th>header3</th><th>header 4</th> </tr></thead>
<tbody><tr class='row'><td>rij 1</td><td>cell 1</td><td class='id'>1</td><td><button value='up' class="up">Up</button> <button value='down' class="down">Down</button></td></tr>
<tr class='row'><td>rij 2</td><td>cell 2</td><td class='id'>2</td><td><button value='up' class="up">Up</button> <button value='down' class="down">Down</button></td></tr>
<tr class='row'><td>rij 3</td><td>cell 3</td><td class='id'>3</td><td><button value='up' class="up">Up</button> <button value='down' class='down'>Down</button></td></tr>
<tr class='row'><td>rij 4</td><td>cell 4</td><td class='id'>4</td><td><button value='up' class="up">Up</button> <button value='down' class='down'>Down</button></td></tr>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function(){
$('.up').on('click',function() {
var parent = $(this).parents(".row");
var parent_id = $(this).parents(".row:first").find('.id').val();
var prev_id = parent.prev('.row:first').find('.id').val();
parent.insertBefore(parent.prev(".row"));
$(this).parents(".row").find('.id').val(prev_id);
parent.prev('.row:first').find('.id').val(parent_id);
});
$('.down').on('click',function(){
var parent = $(this).parents(".row");
var parent_id = $(this).parents(".row").find('.id').val();
var next_id = parent.next('.row').find('.id').val();
parent.insertAfter(parent.next(".row"));
$(this).parents('.row').find('.id').val(next_id);
parent.prev('.row').find('.id').val(parent_id);
})
;
})
</script>
</body>
</html>