Solved

Getting error in FF and Chrome when cloning childnodes

Posted on 2010-11-13
4
562 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
ID: 34129288
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
ID: 34129987
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
ID: 34130017
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
ID: 34132244
Which completely explains why it would fail as written in Chrome and FF but not in IE.  Thanks for the explanation.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

815 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

8 Experts available now in Live!

Get 1:1 Help Now