I have this code, which works:
var document_id;
$(document).delegate('td', 'click', function (event) {
var stop = this.id == "dontcompress" ? 1 : 0;
var stopcheckbox = event.target.nodeName == "INPUT" ? 1 : 0;
if (stop == 0 && stopcheckbox == 0) {
$('[colspan="9"]').parent('tr').remove();
$(this).parents('tr').after('<tr/>').next().append('<td id="dontcompress" colspan="9"/>').children('td').append('<div/>').css('background', '#fff').children().html($('#content').html());
document_id = this.id;
ajax_get(document_id); //fills the content
}
});
This creates a new row in the table right under the pressed row and fills it. This works flawlessly.
I want to be able to do the same with keys so I tried this:
$(document).keydown(function(e) {
switch(e.which) {
case 38: // up
if(!document_id) {
row_id = $('#box_id').find('tr:first').attr('id');
document_id = row_id.split("_").pop();
}else {
row_id = $('#row_'+document_id ).prev("tr").attr("id");
if(row_id){
document_id = row_id.split("_").pop();
}
}
$("#dontcompress").remove();
$("#row_" + document_id).parents('tr').after('<tr/>').next().append('<td id="dontcompress" colspan="9"/>').children('td').append('<div/>').css('background', '#fff').children().html($('#content').html());
ajax_get(document_id);
break;
case 40: //down
if(!document_id) {
row_id = $('#box_id').find('tr:first').attr('id');
document_id = row_id.split("_").pop();
}else {
row_id = $('#row_' + document_id).next("tr").attr("id");
if(row_id) {
document_id = row_id.split("_").pop();
}
}
$("#dontcompress").remove();
$("#row_" + document_id).parents('tr').after('<tr/>').next().append('<td id="dontcompress" colspan="9"/>').children('td').append('<div/>').css('background', '#fff').children().html($('#content').html());
ajax_get(document_id);
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action (scroll / move caret)
});
The point would be to remove the tr if it already exists, and open it a row up if up is pressed, or a row down if down is pressed.
Nothing happens, though.
I have checked the value of document_id with console.log and it gets the ids well.
Any help would be appreciated.
The HTML looks something like this:
<tbody id="box_id">
@foreach($documents as $document)
<tr id="row_{{$document->id}}">
<td id="{{$document->id}}">{{$document->something}}</td>
<td id="{{$document->id}}">{{$document->samthinels}}</td>
</tr>
@endforeach
</tbody>
Any help?