How to insert <td> in <tr> using jquery

I have tablewith several lines, I just need to arrange them, I can get <td>and <tr>, and now I will need to insert them back into the custom order

 $('.white-header').each(function() {  //white-header is the class used for <tr>
    var tr = $(this);

    var td1 = tr.find('td:eq(4)'); // indices are zero-based here
    var td2 = tr.find('td:eq(5)');
    var td3 = tr.find('td:eq(6)');
    var td4 = tr.find('td:eq(7)');
    var td5 = tr.find('td:eq(8)');
    var td6 = tr.find('td:eq(9)');
    var td7 = tr.find('td:eq(10)');
    td1.remove();
    td2.remove();
    td3.remove();
    td4.remove();
    td5.remove();
    td6.remove();
    td7.remove();
    tr.insert(td7); // here am getting errors, i tried .append also
    tr.insert(td6);
    tr.insert(td4);
});

I just need to know how to insert tdinto this tr(currently trempty, I think after deleting everything td)

+3
source share
3 answers

You do not need .remove()at all. All you need to - use appendand prepend(or appendToand prependTo) smart way to reorder your cells. These methods do not copy the DOM nodes, they move them, so deletion is completely unnecessary.

:

$('.white-header').each(function() {
    var tr = $(this);  

    tr.find('td:eq(4)').appendTo(tr);
    tr.find('td:eq(6)').appendTo(tr);
    tr.find('td:eq(9)').prependTo(tr);
});

( , :eq , - )

+1

, <td> <td>,

$('.white-header').each(function() {  //white-header is the class used for <tr>
    var tr = $(this);
    var td1 = tr.find('td:eq(4)'); // indices are zero-based here
    var td2 = tr.find('td:eq(5)');
    var td3 = tr.find('td:eq(6)');
    var td4 = tr.find('td:eq(7)');
    var td5 = tr.find('td:eq(8)');
    var td6 = tr.find('td:eq(9)');
    var td7 = tr.find('td:eq(10)');
    td1.remove();
    td2.remove();
    td3.remove();
    td4Cloned = td4.clone();
    td4.remove();
    td5.remove();
    td6Cloned = td6.clone();
    td6.remove();
    td7Cloned = td7.clone();
    td7.remove();

    tr.insert(td7Cloned); // here am getting errors, i tried .append also
    tr.insert(td6Cloned);
    tr.insert(td4Cloned);
});
0

Try using .detach()

$('.white-header').each(function() {  //white-header is the class used for <tr>
    var tr = $(this);    

    var td1 = tr.find('td:eq(4)').detach(); // indices are zero-based here
    var td2 = tr.find('td:eq(5)');
    var td3 = tr.find('td:eq(6)').detach();
    var td4 = tr.find('td:eq(7)').detach();
    var td5 = tr.find('td:eq(8)');
    var td6 = tr.find('td:eq(9)');
    var td7 = tr.find('td:eq(10)');
    td1.remove();
    td2.remove();
    td3.remove();
    td5.remove();

    tr.append(td7); // here am getting errors, i tried .append also
    tr.append(td6);
    tr.append(td4);    
});
0
source

All Articles