- Oct 9, 2002
- 28,298
- 1,234
- 136
I haven't actually read a JScript book or anything, but I'm trying to accomplish basic tasks by using Google and applying my limited BASIC experience.
Here, I've written yet another bit of code that works perfectly in Chrome/FF, but doesn't work in IE:
- This shows a row containing a textbox and a "Remove" button.
- Below that is a single "Add" button.
- Each time the "Add" button is clicked, another row is added to the DIV above it.
- Each "Remove" button will remove the row that contains it.
- When only 1 row remains, the remaining "Remove" button is disabled and cannot be clicked.
For some reason, in IE, the "Remove" buttons that are added during runtime will not fire their onClick event. This works perfectly in Chrome and Firefox.
I can add this button to the HTML part (so it's not added during runtime). It removes the last row using the same removeRow() function, and works fine in IE:
You guys have been extremely helpful with my past challenges, so maybe you can tell me again: What am I doing wrong?
Thanks!
Here, I've written yet another bit of code that works perfectly in Chrome/FF, but doesn't work in IE:
PHP:
<HTML>
<HEAD>
<TITLE>IE SUCKS!</TITLE>
<SCRIPT language="javascript">
// create global variable to track number of rows
var rowCount=0;
function addRow()
{
// a row is being added, so rowCount must be increased
rowCount+=1;
// div
var divrowelement=document.createElement("div");
divrowelement.setAttribute("id","divrow"+rowCount);
document.getElementById("divcontainingrows").appendChild(divrowelement);
// textbox
var inputelement=document.createElement("input");
inputelement.setAttribute("id","textbox"+rowCount);
inputelement.setAttribute("type","textbox");
inputelement.setAttribute("value",rowCount);
document.getElementById("divrow"+rowCount).appendChild(inputelement);
// button
var deleteelement=document.createElement("input");
deleteelement.setAttribute("id","btndelete"+rowCount);
deleteelement.setAttribute("type","button");
deleteelement.setAttribute("value","Remove");
deleteelement.setAttribute("onClick","removeRow("+rowCount+")");
document.getElementById("divrow"+rowCount).appendChild(deleteelement);
// disable the "Remove" button in row1 if there is only 1 row, prevents user from removing last remaining row
// enable the "Remove" button in row1 if there is more than one row
document.getElementById("btndelete1").disabled=(rowCount==1);
}
function removeRow(rowToRemove)
{
alert("Removing row"+rowCount);
document.getElementById("divrow"+rowToRemove).parentNode.removeChild(document.getElementById("divrow"+rowToRemove));
for (var i=rowToRemove+1;i<=rowCount;i+=1)
{
// for debug purposes
alert("Renaming divrow"+i);
// rename div
document.getElementById("divrow"+i).setAttribute("id","divrow"+(i-1));
// rename textbox
document.getElementById("textbox"+i).setAttribute("value",(i-1));
document.getElementById("textbox"+i).setAttribute("id","textbox"+(i-1));
// rename button
document.getElementById("btndelete"+i).setAttribute("onClick","removeRow("+(i-1)+");");
document.getElementById("btndelete"+i).setAttribute("id","btndelete"+(i-1));
}
// a row has been removed, so rowCount must be decreased
rowCount-=1;
// disable the "Remove" button in row1 if there is only 1 row, prevents user from removing last remaining row
// enable the "Remove" button in row1 if there is more than one row
document.getElementById("btndelete1").disabled=(rowCount==1);
}
</SCRIPT>
</HEAD>
<BODY onload="addRow()">
<FORM>
<div id="divcontainingrows"></div>
<INPUT type="button" value="Add" onclick="addRow()"/>
</FORM>
</BODY>
</HTML>
- This shows a row containing a textbox and a "Remove" button.
- Below that is a single "Add" button.
- Each time the "Add" button is clicked, another row is added to the DIV above it.
- Each "Remove" button will remove the row that contains it.
- When only 1 row remains, the remaining "Remove" button is disabled and cannot be clicked.
For some reason, in IE, the "Remove" buttons that are added during runtime will not fire their onClick event. This works perfectly in Chrome and Firefox.
I can add this button to the HTML part (so it's not added during runtime). It removes the last row using the same removeRow() function, and works fine in IE:
PHP:
<INPUT type="button" value="Remove" onclick="removeRow(rowCount)"/>
You guys have been extremely helpful with my past challenges, so maybe you can tell me again: What am I doing wrong?
Thanks!
Last edited: