473,387 Members | 1,423 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,387 software developers and data experts.

How to handle element IDs (and names) when cloning nodes

I want to use the 'cloneNode(true)' method to duplicate table rows with
all its subnodes. Cloning the complete tree is very handy but I must be
cautious because of the unique IDs and names that I'll subsequently use
to address the original row elements.

Is there any sophisticated way to adjust all the element IDs and names
of the cloned rows so that there won't be any clashes if I subsequently
address them by ID? Or do I have to manually parse the subtree and change
the ID/name attributes of each node separately?

Thanks for any hints!

Janis
Dec 21 '06 #1
2 1680
Janis Papanagnou wrote:
Is there any sophisticated way to adjust all the element IDs and names
of the cloned rows so that there won't be any clashes if I subsequently
address them by ID? Or do I have to manually parse the subtree and change
the ID/name attributes of each node separately?
If you do e.g.
var element = someElement.cloneNode(true);
then to look for all contained elements you can use e.g.
var elements = element.getElementsByTagName('*');
and then check element.id and loop through those elements to check for
ids you want/need to change. Note that IE/Win unfortunately only
supports the wild card '*' with IE 6 and later, for older versions you
could use
var elements = element.all;
--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 21 '06 #2
Martin Honnen wrote:
Janis Papanagnou wrote:
>Is there any sophisticated way to adjust all the element IDs and names
of the cloned rows so that there won't be any clashes if I subsequently
address them by ID? Or do I have to manually parse the subtree and change
the ID/name attributes of each node separately?


If you do e.g.
var element = someElement.cloneNode(true);
then to look for all contained elements you can use e.g.
var elements = element.getElementsByTagName('*');
That's interesting; good to know that wildcards are possible.
and then check element.id and loop through those elements to check for
ids you want/need to change. Note that IE/Win unfortunately only
supports the wild card '*' with IE 6 and later, for older versions you
could use
var elements = element.all;
Okay, so I have to parse that manually, if I understand correct.
So what I've done now is to select all the elements by <inputtag.

active_row = document.getElementById ("Active_Row")
cloned_row = active_row.cloneNode (true);

for (i=0; i<N; i++)
{
element_i = cloned_row.getElementsByTagName ('input')[i]
attr_disabled = document.createAttribute ("disabled")
attr_disabled.nodeValue = "disabled" /* [1] */
element_i.setAttributeNode (attr_disabled)
element_i.removeAttribute ("name") /* [2] */
}

Some questions came up with that code where I am feeling uncertain...

In HTML I've heard it be good style to define an attribute 'disabled' as
<... disabled="disabled" ...>
and I've done the same above in [1]; but is that line also necessary in
Javascript?

To disable the selection by name I just removed the name attribute in [2];
I suppose it is okay to do it that way?

The third question is the most puzzling for me; following the above code
I wanted to insert cloned_row into the table, and while appending worked
fine...

document.getElementById ("Table").appendChild (cloned_row)

trying to _insert_ it before active_row with this code...

document.getElementById ("Table").insertBefore (cloned_row, active_row)

*failed* with an error message in the console:

Error: uncaught exception: [Exception... "Node was not found"
code: "8" nsresult: "0x80530008 (NS_ERROR_DOM_NOT_FOUND_ERR)"
location: "file:... Line: 390"]

But according to some documentation I have that should work. What have I
done wrong?

Thanks for any insights!

Janis
Dec 21 '06 #3

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
9
by: Derek Basch | last post by:
I have a simple XHTML document that I would like to apply javascript functionality to: <body> <div class="length">9</div> <div class="sequence">AAAAAAATTTAAA</div> <div...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
16
by: TT (Tom Tempelaere) | last post by:
Hi all, I created an XSD to define the structure of an XML file for my project. I made an XML file linked to the XSD using XmlSpy. The problem is that if I read the file using .NET XmlDocument...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.