Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting error in FF and Chrome when cloning childnodes

Posted on 2010-11-13
4
Medium Priority
?
566 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
sam2912 earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

705 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