M
mikez302
Hi. I am trying to make a Javascript program that keeps track of
prices, and I'm encountering a bug. Each row in my table has a
number: the top row is numbered 0, the next one is 1, etc. From the
user's point of view, the numbers begin from 1, but that is
irrelevant. Each row has an input whose name attribute contains the
row number: "monthly[0][fee]", "monthly[1][fee]", etc. Each row also
has a button to remove the row. Clicking the remove button is
supposed to call a function so that when a row is removed, the rows
are are renumbered sequentially. For example, when you remove row 1,
row 2 becomes the new row 1, row 3 becomes row 2, etc.
It works fine in Firefox 2.0.0.11, but I am encountering a bug in IE
7.0 and Safari. If I remove the top row, then remove the top row
again, I get a Javascript error in IE saying "'undefined' is null or
not an object.". From then on, I get lots of weird problems, and the
form can't be submitted properly. From my experimentation, it seems
that IE is getting confused about the name attribute of one of the
input boxes immediately after removing the row. Why is this
happening?
You can see it in action at http://eliaszamaria.info/IEBugDemo1.html.
My actual code is more complicated, but this seems to be the simplest
situation where I run into this problem. Click the top remove button,
then click it again, and look at the status bar. I also have a second
page, http://eliaszamaria.info/IEBugDemo2.html, which is exactly the
same, but with alerts showing the type of the 2nd text input (named
"monthly[1][fee]") immediately before and after removing the row (but
before renumbering the text input). In Firefox, it shows up as
"[object HTMLInputElement]" the whole time. However, in IE, after
clicking the remove button for the 2nd time, it shows up as "[object]"
before removing the row, and "undefined" after, even though removing
the 1st row shouldn't touch the 2nd text input. Safari gives similar
results. Can anyone figure out why this is happening, or suggest a
way to work around the problem? I looked on Google and found lots of
info about Javascript bugs in IE, but nothing helpful for my specific
situation.
Here is the code I'm using:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv='Content-Type' content='text/html;
charset=ISO-8859-1'>
<meta http-equiv='Content-Language' content='en-us'>
<title>IE removeChild bug demo</title></head>
<body>
<script type='text/javascript'>var monthlyFees = [{"fee":"250.00"},
{"fee":"195.00"},{"fee":"125.00"}];
function removeFee(rowNum) {
var i, testBody = document.getElementById('fees').tBodies[0];
testBody.removeChild(testBody.rows[rowNum]);
for (i = rowNum; i < testBody.rows.length; i++) {
testBody.rows.cells[0].innerHTML = "<span class='rowNumber'>" +
(parseInt(i) + 1) + "</span> <input type='button' value='Remove'
class='remove' onclick='removeFee(" + i + ")'>";
document.edit.elements['monthly[' + (parseInt(i) + 1) + ']
[fee]'].name = document.edit.elements['monthly[' + i + '][fee]'];
}
}
</script>
<form method='post' action='' name='edit'>
<table id='fees'>
<tr><td><span class="rowNumber">1</span> <input value="Remove"
onclick="removeFee(0)" type="button"></td><td><input name="monthly[0]
[fee]" size="11" maxlength="11" value="250.00" type="text"></td></tr>
<tr><td><span class="rowNumber">2</span> <input value="Remove"
onclick="removeFee(1)" type="button"></td><td><input name="monthly[1]
[fee]" size="11" maxlength="11" value="195.00" type="text"></td></tr>
<tr><td><span class="rowNumber">3</span> <input value="Remove"
onclick="removeFee(2)" type="button"></td><td><input name="monthly[2]
[fee]" size="11" maxlength="11" value="125.00" type="text"></td></tr></
table></form>
</body></html>
I appreciate your help.
Elias
prices, and I'm encountering a bug. Each row in my table has a
number: the top row is numbered 0, the next one is 1, etc. From the
user's point of view, the numbers begin from 1, but that is
irrelevant. Each row has an input whose name attribute contains the
row number: "monthly[0][fee]", "monthly[1][fee]", etc. Each row also
has a button to remove the row. Clicking the remove button is
supposed to call a function so that when a row is removed, the rows
are are renumbered sequentially. For example, when you remove row 1,
row 2 becomes the new row 1, row 3 becomes row 2, etc.
It works fine in Firefox 2.0.0.11, but I am encountering a bug in IE
7.0 and Safari. If I remove the top row, then remove the top row
again, I get a Javascript error in IE saying "'undefined' is null or
not an object.". From then on, I get lots of weird problems, and the
form can't be submitted properly. From my experimentation, it seems
that IE is getting confused about the name attribute of one of the
input boxes immediately after removing the row. Why is this
happening?
You can see it in action at http://eliaszamaria.info/IEBugDemo1.html.
My actual code is more complicated, but this seems to be the simplest
situation where I run into this problem. Click the top remove button,
then click it again, and look at the status bar. I also have a second
page, http://eliaszamaria.info/IEBugDemo2.html, which is exactly the
same, but with alerts showing the type of the 2nd text input (named
"monthly[1][fee]") immediately before and after removing the row (but
before renumbering the text input). In Firefox, it shows up as
"[object HTMLInputElement]" the whole time. However, in IE, after
clicking the remove button for the 2nd time, it shows up as "[object]"
before removing the row, and "undefined" after, even though removing
the 1st row shouldn't touch the 2nd text input. Safari gives similar
results. Can anyone figure out why this is happening, or suggest a
way to work around the problem? I looked on Google and found lots of
info about Javascript bugs in IE, but nothing helpful for my specific
situation.
Here is the code I'm using:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv='Content-Type' content='text/html;
charset=ISO-8859-1'>
<meta http-equiv='Content-Language' content='en-us'>
<title>IE removeChild bug demo</title></head>
<body>
<script type='text/javascript'>var monthlyFees = [{"fee":"250.00"},
{"fee":"195.00"},{"fee":"125.00"}];
function removeFee(rowNum) {
var i, testBody = document.getElementById('fees').tBodies[0];
testBody.removeChild(testBody.rows[rowNum]);
for (i = rowNum; i < testBody.rows.length; i++) {
testBody.rows.cells[0].innerHTML = "<span class='rowNumber'>" +
(parseInt(i) + 1) + "</span> <input type='button' value='Remove'
class='remove' onclick='removeFee(" + i + ")'>";
document.edit.elements['monthly[' + (parseInt(i) + 1) + ']
[fee]'].name = document.edit.elements['monthly[' + i + '][fee]'];
}
}
</script>
<form method='post' action='' name='edit'>
<table id='fees'>
<tr><td><span class="rowNumber">1</span> <input value="Remove"
onclick="removeFee(0)" type="button"></td><td><input name="monthly[0]
[fee]" size="11" maxlength="11" value="250.00" type="text"></td></tr>
<tr><td><span class="rowNumber">2</span> <input value="Remove"
onclick="removeFee(1)" type="button"></td><td><input name="monthly[1]
[fee]" size="11" maxlength="11" value="195.00" type="text"></td></tr>
<tr><td><span class="rowNumber">3</span> <input value="Remove"
onclick="removeFee(2)" type="button"></td><td><input name="monthly[2]
[fee]" size="11" maxlength="11" value="125.00" type="text"></td></tr></
table></form>
</body></html>
I appreciate your help.
Elias