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; which result in that the new row will be inserted at the last position. |
Browser Support
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>
<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>
[출처] Table insertRow() Method|작성자 zumerfolg
'Programming > JavaScript' 카테고리의 다른 글
[jquery]jquery widget example (0) | 2014.03.07 |
---|---|
문자 byte 수 체크하기 (0) | 2013.12.05 |
ajax 사용법 (0) | 2013.11.08 |
동적 폼 추가 삭제 (0) | 2013.10.30 |
[jquery] jquery 로 체크박스 리스트의 체크된 값들 가져오기 (0) | 2013.09.13 |