<html>
<head>
<title> Dynamic add or delete rows of a table </TITLE>
<script language = "javascript">
function getTable() {
return document.getElementById("testid");
}
function addRow() {
var cell0_html = '<a href=# οnclick=/"deleteRow(this)/">delete</a>';
var cell1_html = '<input type=/"text/">';
var tbl = getTable();
var row;
var cell;
// add new row after the last row -- TR
row = tbl.insertRow(tbl.rows.length);
// create a cell for the new row -- TD
cell = tbl.rows.item(tbl.rows.length - 1).insertCell(0);
cell.innerHTML = cell0_html;
cell = tbl.rows.item(tbl.rows.length - 1).insertCell(1);
cell.innerHTML = cell1_html;
var rowIdx = tbl.rows.length -1 ; // first row -- 0
// store the row index into the related row
// row's first cell's first child -- refer to "<a href=#"
tbl.rows.item(tbl.rows.length-1).cells.item(0).children.item(0).text = rowIdx;
alert("rowIdx="+rowIdx);
}
function deleteRow(x) {
var tbl = getTable();
// delete the row by row index
tbl.deleteRow(x.text.valueOf());
// refresh the row index
for (var j = 1; j < tbl.rows.length; j++)
tbl.rows.item(j).cells.item(0).children.item(0).text=j;
}
function refreshIndex() {
// refresh the row index
for (var j = 1; j < tbl.rows.length; j++)
tbl.rows.item(j).cells.item(0).children.item(0).text=j;
}
</script>
</head>
<body>
<input type="button" value="add" οnclick="addRow()">
<table width="100%" id="testid" border=1>
<tr>
<td>H-1</td>
<td>H-2</td>
</tr>
</table>
</body>
</html>