196
rated 0 times
[
199]
[
3]
/ answers: 1 / hits: 37594
/ 12 Years ago, thu, august 30, 2012, 12:00:00
I want to add/remove rows in a table dynamically. I have javascript function to add and remove the rows. But, I want the delete button beside every single row so that I can delete a particular row.
ANd I want to add a row only if the first row is completely filled.
function to remove row
function removeRowFromTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
function to add rows:
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement(input);
element1.type = text;
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
var element2 = document.createElement(input);
element2.type = text;
cell2.appendChild(element2);
}
my table:
<table id=tableId>
<tr><td>Host Name</td><td>Directory</td></tr>
<tr><td><input type=text/></td><td><input type=text/></td></tr>
<tr><td><input type=button value=+ onclick=addRow(tableId)/></td>
<td><input type=button value=- onclick=removeRowFromTable()/></td></tr>
</table>
Any help is appreciated! Thanks in Advance!!!
More From » html