473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

modifying childNodes of a cloned div

Hi,
I am modifying some code from here
http://www.quirksmode.org/dom/domform.html

I have a div 'readroot' that I clone. I change the change the id and
name of the childnodes of 'readroot' to the original name plus a
number(counter).

The problem is I have i have a div 'serials' inside 'readroot' and the
childnodes of
'readroot' are not modified with the current function i have.

What is the best way to modify the childnodes of the div serials?

I guess i could put another for loop inside the for loop of the
moreFields function
but i am thinking a recursive function could do it.

I am still a javascript beginner so i may be missing something obvious.

Thanks for your time,
Chuck


<script type="text/javascript">
var counter = 0;
function moreFields()
{ counter++;
var newFields = document.getElementById('readroot').cloneNode(true );
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++)
{ var theID = newField[i].id;
if(theID) newField[i].id = theID + counter;
var theName = newField[i].name;
if (theName) newField[i].name = theName + '[' + counter + ']';
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,inser tHere);
}

window.onload = moreFields;
</script>

<div id="readroot" style="display: none" >
<input id="name" name="name" type="text" />
<input id="phone" name="phone" type="text" />
<input id="email" name="email" type="text" />
<div id="serials">
<input id="serialnumb" name="sn" type="text" />
<input id="warrantystatus" name="ws" type="text" />
<input id="condition" name="c" />
</div>
</div>

<form>
<span id="writeroot"></span>

<input type="button" value="Give me more fields!" onClick="moreFields()">

</form>
Nov 23 '05 #1
2 2542
chuck wrote:
Hi,
I am modifying some code from here
http://www.quirksmode.org/dom/domform.html

I have a div 'readroot' that I clone. I change the change the id and
name of the childnodes of 'readroot' to the original name plus a
number(counter).

The problem is I have i have a div 'serials' inside 'readroot' and the
childnodes of
'readroot' are not modified with the current function i have.

What is the best way to modify the childnodes of the div serials?

I guess i could put another for loop inside the for loop of the
moreFields function
but i am thinking a recursive function could do it.
Recursion is handy, but sometimes slow. Have you considered using
getElementsByTagName()?
e.g. Call the function with:

changeIds(newFields, counter);
...
And the function is:

function changeIds(r, c)
{
var rKids = r.getElementsByTagName('*');
var i = rKids.length;
var rKid;
while (i--){
rKid = rKids[i]
if (rKid.id) rKid.id = rKid.id + c;
if (rKid.name) rKid.name = rKid.name + '[' + c + ']';
}
}

Some older versions of IE don't support '*' so you may need to feature
test and use an alternative.

An alternative recursive function would look something like:

changeIds(newFields, counter);
...
function changeIds(r, c)
{
var rKid, rKids = r.childNodes;
var i = rKids.length;
while (i--){
rKid = rKids[i];
if (rKid.id) rKid.id = rKid.id + c;
if (rKid.name) rKid.name = rKid.name + '[' + c + ']';
if (rKid.childNodes) changeIds(rKid, c);
}
}
Tested in Firefox & IE. If you have any problems, post again.

I am still a javascript beginner so i may be missing something obvious.

Thanks for your time,
Chuck

<script type="text/javascript">
var counter = 0;
function moreFields()
{ counter++;
var newFields = document.getElementById('readroot').cloneNode(true );


changeId(newFields, counter);
}

And you're done.

[...]

--
Rob
Nov 23 '05 #2
RobG wrote:
chuck wrote:
What is the best way to modify the childnodes of the div serials?
Tested in Firefox & IE. If you have any problems, post again.


Thanks Rob!
chuck
Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: sonic | last post by:
Hello, I am cloning a table row which contains images that have behaviors attached to them as well as onclick events. The problem is that the cloned row seems to be executing the...
1
by: ason | last post by:
Hi, I just tried to replace a single XmlNode with several ChildNodes. When doing this with the following code i found out that if you get the ChildNodes with 'ChildNodes' - property the nodes...
0
by: deko | last post by:
For some reason this is eluding me... I have a subform datasheet that contains appointments imported form Outlook. If the appointment is associated with an Entity in the database, it will have a...
3
by: Q1tum | last post by:
Hi all, I have a problem with getting the amount of childs in a XML structure, the strucure is somewhat like the following: <?xml version="1.0" encoding="iso-8859-1"?> <cms> <num>21</num>...
1
by: yawnmoth | last post by:
Given an element ID, is there a way to figure out what index one would need to use in the parentNode's childNodes array to get at that element? For example... <body> <div id="parent"> <div...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
5
by: Moses | last post by:
HI The Value for childNodes.length differs with mozilla and IE Is it problem with my coding..... I could not under stood............. The following is the details
11
by: priyapratheep | last post by:
Hi friends, I am strugging with this problem.I don't know much javascript. Please you gurus can help me. My problem is I cloned the frist row in a table.after cloning i want to change the names...
2
by: willyWEB66 | last post by:
Hi everyone, I have this code in javascript where the XML file is loaded and displayed to an html using XSLT. It works fine in IE but not in Firefox. My problem is in the looping to the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.