Search w3schools.com:

SHARE THIS PAGE

Table insertRow() Method

Table Object Reference Table Object

Definition and Usage

The insertRow() method inserts a new row at the specified index in a table.

Syntax

tableObject.insertRow(index)

Value Description
index

An integer that specifies the position of the row to insert (starts at 0).

The value of -1 can also be used, this results in a new row being inserted at the last position.

This parameter is required in Firefox and Opera, but optional in Internet Explorer, Chrome and Safari.

If this parameter is omitted, insertRow() inserts a new row at the last position in IE and at the first position in Chrome and Safari.



Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The insertRow() method is supported in all major browsers.


Example

Example

Insert new rows at the first position of the table:

<html>
<head>
<script>
function displayResult()
{
var table=document.getElementById("myTable");
var row=table.insertRow(0);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
cell1.innerHTML="New";
cell2.innerHTML="New";
}
</script>
</head>
<body>

<table id="myTable" border="1">
  <tr>
    <td>cell 1</td>
    <td>cell 2</td>
  </tr>
  <tr>
    <td>cell 3</td>
    <td>cell 4</td>
  </tr>
</table>
<br>
<button type="button" onclick="displayResult()">Insert new row</button>

</body>
</html>

Try it yourself »


Table Object Reference Table Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]