Solved

Getting error in FF and Chrome when cloning childnodes

Posted on 2010-11-13
4
555 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:saabStory
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
sam2912 earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:saabStory
Comment Utility
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
 
LVL 14

Expert Comment

by:sam2912
Comment Utility
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
 

Author Comment

by:saabStory
Comment Utility
Which completely explains why it would fail as written in Chrome and FF but not in IE.  Thanks for the explanation.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now