I am having trouble with my row numbers on the table to re arrange them selves to the proper order when a row is deleted from.
For example
1
2
3
4
When i delete row 3 the order looks like this
1
2
4
When i add a row it then looks like this
1
2
4
4
I would like it to have renumbered itself to account for the deletion
1
2
3
4
Here is my code.
Javascript
HTML Code
For example
1
2
3
4
When i delete row 3 the order looks like this
1
2
4
When i add a row it then looks like this
1
2
4
4
I would like it to have renumbered itself to account for the deletion
1
2
3
4
Here is my code.
Javascript
Code:
<script language="javascript">
function deleteIt(i)
{
document.getElementById('table1').deleteRow(i)
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteIt(lastRow - 1);
}
function addRow()
{
var tbody = document.getElementById("table1").getElementsByTagName("tbody")[0];
var lastRow = table1.rows.length;
var iteration = lastRow + 1;
var row = document.createElement("TR");
var cell0 = document.createElement("TD");
var inp0 = document.createTextNode(iteration);
cell0.appendChild(inp0);
var cell1 = document.createElement("TD");
var inp1 = document.createElement("INPUT");
inp1.setAttribute("type","text");
inp1.setAttribute("value","");
inp1.setAttribute("name",'txt1Row' + iteration);
inp1.setAttribute("id",'txt1Row' + iteration);
cell1.appendChild(inp1);
var cell2 = document.createElement("TD");
var inp2 = document.createElement("INPUT");
inp2.setAttribute("type","text");
inp2.setAttribute("size","3");
inp2.setAttribute("value","");
inp2.setAttribute("name",'txt2Row' + iteration);
inp2.setAttribute("id",'txt2Row' + iteration);
cell2.appendChild(inp2);
var cell3 = document.createElement("TD");
var inp3 = document.createElement("INPUT");
inp3.setAttribute("type","text");
inp3.setAttribute("size","3");
inp3.setAttribute("value","");
inp3.setAttribute("name",'txt3Row' + iteration);
inp3.setAttribute("id",'txt3Row' + iteration);
cell3.appendChild(inp3);
var cell4 = document.createElement("TD");
var inp4 = document.createElement("INPUT");
inp4.setAttribute("type","image");
inp4.setAttribute("src","images/delete.gif");
inp4.setAttribute("value","Delete");
inp4.onclick = function (){deleteIt(this.parentNode.parentNode.rowIndex);}
cell4.appendChild(inp4);
row.appendChild(cell0);
row.appendChild(cell1);
row.appendChild(cell2);
row.appendChild(cell3);
row.appendChild(cell4);
tbody.appendChild(row);
//alert(row.innerHTML);
}
</script>
HTML Code
Code:
<table id="table1">
<tbody>
<tr>
<td>1</td>
<td><input type="text" value="" name="txt1Row1"></td>
<td><input type="text" value="" size="3" name="txt2Row1"></td>
<td><input type="text" value="" size="3" name="txt3Row1"></td>
<td><input type="image" value="Delete" onclick="deleteIt(this.parentNode.parentNode.rowIndex)" src="images/delete.gif"></td>
</tr>
</tbody>
</table>
<input type="button" value="Insert Row" onClick="addRow();">
<input type="submit" value="Submit">