hi all
i have taken from internet a script that adds and removes rows
but i made a little changes to that script that is renumbering rows after deleting.
but one problem arises. i can delete all rows by selecting at once except firts row. for ex. i have added 10 rows and i chose 9-th of them i delete them with no problem. but when i choose the first row it deletes one by one and displays error.
my script is below
PHP Code:
<HTML>
<HEAD>
<TITLE> Add/Remove dynamic rows in HTML table </TITLE>
<SCRIPT language="javascript">
var rowCount = 0;
function addRow(tableID) {
rowCount++;
var table = document.getElementById(tableID);
var z=table.rows.length;
if(rowCount>z)
rowCount=z;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;
var cell3 = row.insertCell(2);
var element2 = document.createElement("input");
element2.type = "text";
cell3.appendChild(element2);
}
function delete() {
try {
var table = document.getElementById('dataTable');
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = table.rows[i].cells[0].childNodes[0];
var soz = table.rows[i].cells[1];
if(null != chkbox && true == chkbox.checked) {
if(
table.deleteRow(i);
rowCount--;
i--;
That's a tricky one, but I think I found it. If you delete the first row, i is 0 so the i-- line in the middle of the loop will make i = -1. Then, the call for table.rows[i].cells[1].childNode[0] produces the error because table.rows[-1] is undefined. Just replace that line with this:
(as a side note, chrome at least considers the word "delete" reserved so don't use it as a function name . Also, you have a random line that read "if(" in that script, but I assume you fixed that )
Bookmarks