Link to home
Start Free TrialLog in
Avatar of saabStory
saabStoryFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Samuel Liew
Samuel Liew
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of saabStory

ASKER

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.
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];
Which completely explains why it would fail as written in Chrome and FF but not in IE.  Thanks for the explanation.