Getting error in FF and Chrome when cloning childnodes

I found a form on EE (from devic I believe) that I'm trying to modify for my own purposes.  Works great in IE but when I run it in FF and Chrome I get conflicting errors.

In Chrome the error is Object #<a Text> has no method 'getElementsByTagName'

In FF the error is: obj.getElementsByTagName is not a function Line 39

The code I'm testing is below.  I've been poking at it but can't see where the problem is and am hoping for some assistance on this.  Much thanks in advance.

 
<html>
<head>
<title></title>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
var newContacts=[];
function addRow()
{
var tbl=document.getElementById("addContact").childNodes[0];
var newNode = newContacts[newContacts.length] = tbl.cloneNode(true);
tbl.parentNode.appendChild(newNode);
document.getElementById("delRow").disabled=false;
document.getElementById("delRow").style.display="";
clearFields(newContacts[newContacts.length-1]);
}

function addEditRow()
{
var tbl=document.getElementById("addContact").childNodes[0];
var newNode = newContacts[newContacts.length] = tbl.cloneNode(true);
newNode.document.getElementById('btnDelete').style.display = 'none';
tbl.parentNode.appendChild(newNode);
document.getElementById("delRow").disabled=false;
document.getElementById("delRow").style.display="";
clearFields(newContacts[newContacts.length-1]);
}
function deleteRow()
{
newContacts[newContacts.length-1].parentNode.removeChild(newContacts[newContacts.length-1])
newContacts.splice(newContacts.length-1, 1)
if(newContacts.length<1)
{
document.getElementById("delRow").disabled=true;
document.getElementById("delRow").style.display="none";
}
}
function clearFields(obj)
{
var inputs=obj.getElementsByTagName("input");
for(var i=0;i<inputs.length;i++)
{
inputs[i].value="";
}
}

//-->
</SCRIPT>
</head>
<body>
<DIV>
<table style="border-top:0;" cellspacing="0" cellpadding="0" border="0" width="50%">
<tr>
<td>
<fieldset >
<legend>Donor Information</legend>
<table cellspacing="0" cellpadding="0" border="0" width="50%" id="addContact">
<tr id="cloneThis">
<td colspan="2">
<table cellspacing="0" cellpadding="3" border="0" width="100%">
<tr>
<td>First Name:</td>
<td><input type="text" name="contactFName" value=""></td>
</tr>
<tr>
<td>Title:</td>
<td><input type="text" name="contactTitle" value=" "></td>
</tr>
</table>

</td>
</tr>
</table>
</fieldset>
</td>
</tr>
<tr>
<td>
<div style="float:left;"><input class="btnContact" type=button onclick="addRow();" value="Add another contact?"></div>
<div id="testLayer1" style="float:left;"><input class="btnCancel" type=button id="delRow" name="delRow" onclick="deleteRow();" value="Cancel" disabled style="display:none"></div>
</td>
</tr>     
</table>
</div>

</body>
</html>

Open in new window

saabStoryAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Samuel LiewConnect With a Mentor Commented:
Try this and let me know if it works (or not)
<html>
<head>
<title></title>
<script type="text/javascript">
<!--
var newContacts = [];

function addRow() {
	var tbl = document.getElementById("addContact").getElementsByTagName("tr")[0];
	newContacts[newContacts.length] = tbl.cloneNode(true);
	var newNode = newContacts[newContacts.length-1];
	tbl.parentNode.appendChild(newNode);
	document.getElementById("delRow").disabled = false;
	document.getElementById("delRow").style.display = "";
	clearFields(newNode);
}
function addEditRow() {
	var tbl = document.getElementById("addContact").getElementsByTagName("tr")[0];
	newContacts[newContacts.length] = tbl.cloneNode(true);
	var newNode = newContacts[newContacts.length-1];
	newNode.document.getElementById('btnDelete').style.display = 'none';
	tbl.parentNode.appendChild(newNode);
	document.getElementById("delRow").disabled = false;
	document.getElementById("delRow").style.display = "";
	clearFields(newNode);
}
function deleteRow() {
	newContacts[newContacts.length - 1].parentNode.removeChild(newContacts[newContacts.length - 1])
	newContacts.splice(newContacts.length - 1, 1)
	if (newContacts.length < 1) {
		document.getElementById("delRow").disabled = true;
		document.getElementById("delRow").style.display = "none";
	}
}
function clearFields(obj) {
	var inputs = obj.getElementsByTagName("input");
	for (var i = 0; i < inputs.length; i++) {
		inputs[i].value = "";
	}
}
//-->
</script>
</head>
<body>
<div>
	<table style="border-top:0;" cellspacing="0" cellpadding="0" border="0" width="50%">
		<tr>
			<td><fieldset >
					<legend>Donor Information</legend>
					<table cellspacing="0" cellpadding="0" border="0" width="50%" id="addContact">
						<tr id="cloneThis">
							<td colspan="2"><table cellspacing="0" cellpadding="3" border="0" width="100%">
									<tr>
										<td>First Name:</td>
										<td><input type="text" name="contactFName" value=""></td>
									</tr>
									<tr>
										<td>Title:</td>
										<td><input type="text" name="contactTitle" value=" "></td>
									</tr>
								</table></td>
						</tr>
					</table>
				</fieldset></td>
		</tr>
		<tr>
			<td><div style="float:left;">
					<input class="btnContact" type=button onclick="addRow();" value="Add another contact?">
				</div>
				<div id="testLayer1" style="float:left;">
					<input class="btnCancel" type=button id="delRow" name="delRow" onclick="deleteRow();" value="Cancel" disabled style="display:none">
				</div></td>
		</tr>
	</table>
</div>
</body>
</html>

Open in new window

0
 
saabStoryAuthor Commented:
Works great - have no idea why.  I see the additional code but don't know how it's working here.  Have to go do some more research to understand.  Thanks so much for the help - this will be a real help to the people who will be using the form it's for.
0
 
Samuel LiewCommented:
Further explanation:

This part of code fails
document.getElementById("addContact").childNodes[0];

because in a table, if a <tbody> is not specified, some browsers will automatically add it in dynamically. childNodes will or may also refer to the first text node (in this case is blank/whitespace) before the next tag, which is why the string error.

You want to get the table row, so just simply change it to
document.getElementById("addContact").getElementsByTagName("tr")[0];
0
 
saabStoryAuthor Commented:
Which completely explains why it would fail as written in Chrome and FF but not in IE.  Thanks for the explanation.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.