The following basic script works fine in firefox by not in IE. Can anyone
spot the problem? In IE I can only delete the first line but not the lines
created by javascript. Also, look at the HTML code for the first line
(click the Table HTML button:)) you will fine that the code displayed is not
the same as
was written. "onChange" was changed to "onchange" etc. Please help.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>testing tables</title>
<script type="text/javascript">
<!-- Begin Add Row to Itemlist Table
function addRowToTable(){
var tbl = document.getElementById('itemlist');
var lastRow = tbl.rows.length;
var row = tbl.insertRow(lastRow);
// qty cell
var qty2 = row.insertCell(0);
var el_qty = document.createElement('input');
el_qty.setAttribute('type', 'text');
el_qty.setAttribute('name', 'qty');
el_qty.setAttribute('size', '7');
el_qty.setAttribute('maxlength', '7');
el_qty.setAttribute('onChange', 'calc_total()');
qty2.appendChild(el_qty);
// price cell
var price2 = row.insertCell(1);
var el_price = document.createElement('input');
el_price.setAttribute('type', 'text');
el_price.setAttribute('name', 'cost');
el_price.setAttribute('size', '7');
el_price.setAttribute('maxlength', '7');
el_price.setAttribute('onChange', 'calc_total()');
price2.appendChild(el_price);
// remove me cell
var D = row.insertCell(2);
//D.innerHTML='<a href="javascript:removeRowFromTable(this)">-</a>'
D.setAttribute('onClick','removeRowFromTable(this) ');
var el_txt = document.createTextNode("-");
D.appendChild(el_txt);
}
//
function getRowIndex (cell) {
return document.all ? cell.parentElement.rowIndex :
cell.parentNode.rowIndex;
}
//Remove a row from the table
function removeRowFromTable(cell){
//alert ("start function");
var tbl = document.getElementById('itemlist');
var lastRow = tbl.rows.length;
//alert (lastRow);
var rowToDelete = getRowIndex(cell);
//alert (rowToDelete);
if (lastRow > 2) tbl.deleteRow(rowToDelete);
}
// Calculate the total
function calc_total() {
//Do Nothing yet
}
function showtableinnerhtml(){
alert(document.getElementById('itemlist').innerHTM L);
}
//-->
</script>
</head>
<body>
<form name="myform" method="post" action="myform.php">
<table id="itemlist" border="1">
<tr><th>Qty</th><th>Cost</th><th>-</th></tr>
<tr>
<td>
<input type="text" name="qty" size="7" maxlength="7"
onChange="calc_total()">
</td>
<td>
<input type="text" name="cost" size="7" maxlength="7"
onChange="calc_total()">
</td>
<td onClick="removeRowFromTable(this)">-</td>
</tr>
</table>
<hr>
<table id="totalcost" border="1">
<tr>
<td>
<input type="text" size="7" name="test" readonly>
</td>
<td>
<input type="text" size="7" name="total" readonly>
</td>
</tr>
</table>
<input type="button" value="TableHTML" onclick="showtableinnerhtml()">
<input type="button" value="Add" onclick="addRowToTable();">
</form>
</body>
</html>