Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insertRow vs. appendChild

Which method is preferred for adding a row to a table?

var tr = tbl.insertRow(-1);

or

var tr = document.createElement('tr'); tbl.appendChild(tr);

?

like image 539
erikkallen Avatar asked May 18 '09 20:05

erikkallen


People also ask

How do you insert 3 rows before the last row of a table?

Click in a cell above or below where you want to add a row. Under Table Tools, on the Layout tab, do one of the following: To add a row above the cell, click Insert Above in the Rows and Columns group. To add a row below the cell, click Insert Below in the Rows and Columns group.

How to append new row in table using javascript?

The insertRow() method creates an empty <tr> element and adds it to a table. The insertRow() method inserts the new row(s) at the specified index in the table. Note: A <tr> element must contain one or more <th> or <td> elements. Tip: Use the deleteRow() method to remove a row.

How to insert row in HTML?

Insert new row(s) at the beginning of a table. The insertRow() method inserts a new row at the specified index in a table, in this example, the first position (the beginning) of a table with id="myTable". Then we use the insertCell() method to add cells in the new row.

What is insertCell in Javascript?

insertCell() method inserts a new cell ( <td> ) into a table row ( <tr> ) and returns a reference to the cell. Note: insertCell() inserts the cell directly into the row. The cell does not need to be appended separately with Node. appendChild() as would be the case if Document.


1 Answers

insertRow would be the much better. It is supported by grade A browsers and it's less verbose and a cleaner API.

like image 62
aleemb Avatar answered Sep 27 '22 02:09

aleemb