32
rated 0 times
[
38]
[
6]
/ answers: 1 / hits: 27064
/ 11 Years ago, sun, december 1, 2013, 12:00:00
I have an HTML table with insert and delete row functionality and its working perfectly. But delete functionality works with checkbox + delete button.
When i want to delete a row, first i checked the checkbox and then press delete button. I want to make it directly with delete button. Below is my code,
function deleteRow(tableID)
{
try
{
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++)
{
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if (null != chkbox && true == chkbox.checked)
{
if (rowCount <= 1)
{
alert(Cannot delete all the rows.);
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
} catch(e)
{
alert(e);
}
getValues();
}
<a onclick=deleteRow('dataTable')>Delete Row</a>
<table id=dataTable>
<tr>
<td><input type=checkbox name=chk/></td>
<td><input type=text name=Name></td>
</tr>
</table>
Note : Atleast 1 row should be there (Cannot delete all the rows)
More From » html