简体   繁体   中英

what's the easiest method to append a TR to a table by javascript?

If the table id is known – so the table can be obtained with docoument.getElementById(table_id) – how can I append a TR element to that table in the easiest way?

The TR is as follows:

<tr><td><span>something here..</span></td></tr>

The first uses DOM methods, and the second uses the non-standard but widely supprted innerHTML

var tr = document.createElement("tr");
var td = document.createElement("td");
var span = document.createElement("span");
var text = document.createTextNode("something here..");
span.appendChild(text);
td.appendChild(span);
tr.appendChild(td);

tbody.appendChild(tr);

OR

tbody.innerHTML +=  "<tr><td><span>something here..</span></td></tr>"

Using jQuery:

$('#table_id > tbody').append('<tr><td><span>something here..</span></td></tr>');

I know some may cringe at the mention of jQuery. Including a framework to do just this one thing is probably overkill. but I rarely find that I only need to do "just one thing" with javascript. The hand-coded solution is to create each of the elements required, then add them in the proper sequence (from inner to outer) to the other elements, then finally add the new row to the table.

The most straightforward, standards compliant and library-independent method to insert a table row is using the insertRow method of the table object.

var tableRef = document.getElementById(tableID);

// Insert a row in the table at row index 0
var newRow   = tableRef.insertRow(0);

PS Works in IE6 too, though it may have some quirks at times.

If you're not opposed to using jQuery, you can use either of the following where "tblId" is the id of your table and "_html" is a string representation of your table row:

  $(_html).insertAfter("#tblId tr:last"); 

or

$("#tblId tr:last").after(_html); 

Really simple example:

<html>
<table id = 'test'>
   <tr><td>Thanks tvanfosson!</td></tr>
</table>
</html>

<script type="text/javascript" charset="utf-8">
   var table = document.getElementById('test');
   table.innerHTML += '<tr><td><span>something here..</span></td></tr>';
</script>

i use this function to append a bunch of rows into a table. its about 100% faster then jquery for large chunks of data. the only downside is that if your rows have script tags inside of them, the scripts wont be executed on load in IE

function appendRows(node, html){
    var temp = document.createElement("div");
    var tbody = node.parentNode;
    var nextSib = node.nextSibling;

    temp.innerHTML = "<table><tbody>"+html;
    var rows = temp.firstChild.firstChild.childNodes;

    while(rows.length){
        tbody.insertBefore(rows[0], nextSib);
    }
}

where node is the row to append after, and html is the rows to append

I use this, works softly:

var changeInnerHTMLOfMyCuteTbodyById = function (id_tbody, inner_html)
{ 
     //preparing
     var my_tbody = document.getElementById (id_tbody);
     var my_table = my_tbody.parentNode;

     my_table.removeChild (my_tbody);

     //creating dom tree
     var html = '<table style=\'display:none;\'><tbody id='+ id_tbody+'>' + 
inner_html + '</tbody></table>';

     var tmp_div = document.createElement ('div');

     tmp_div.innerHTML = html;

     document.body.appendChild (tmp_div);

     //moving the tbody
     my_table.appendChild (document.getElementById (id_tbody));
}

You can do this:

changeInnerHTMLOfMyCuteTbodyById('id_tbody', document.getElementById ('id_tbody').innerHTML + '<tr> ... </tr>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM