473,513 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy nodes and increment id's

Hi All,
I have a web page which builds a hidden table, as (based on certain
criteria) people can have multiple version of this table, not a
problem. But I need each table to have a unique id (that I know before
hand) and unique id's for each element (the id="" tags are assigned in
the original hidden table).
So I can't used CloneNode as this keeps the id's, can someone point
me in the right direction for a script which will copy nodes +
attributes (as far down as they go) (for any object type, as I might
change it from a table to a div in the future) and append a passed
variable to each id (if id!=null).
e.g. var newTable=copyNode(originalTab,tableId++)
where originalTab is a ref to the original table (might not
be an table in future, i.e. could be any HTML object)
tableId will be a unique number
Any help much appreciated

Paul

Oct 8 '06 #1
1 8447

Pauljh wrote:
Hi All,
I have a web page which builds a hidden table, as (based on certain
criteria) people can have multiple version of this table, not a
problem. But I need each table to have a unique id (that I know before
hand) and unique id's for each element (the id="" tags are assigned in
the original hidden table).
So I can't used CloneNode as this keeps the id's, can someone point
me in the right direction for a script which will copy nodes +
attributes (as far down as they go) (for any object type, as I might
change it from a table to a div in the future) and append a passed
variable to each id (if id!=null).
Clone the node, then use a recursive function to change all the child
IDs.

e.g.

<script type="text/javascript">

function cloneEl(oldID, newID) {
var el = document.getElementById(oldID);
var newEl = el.cloneNode(true);
var oldPattern = oldID + '_';
var newPattern = newID + '_';
newEl.id = newID;
modIDs(newEl, oldPattern, newPattern);
el.parentNode.insertBefore(newEl, el.nextSibling);
}

function modIDs(node, oldPattern, newPattern) {
var child, nodes = node.childNodes;
for (var i=0, len=nodes.length; i<len; i++){
child = nodes[i];
if ('string' == typeof child.id){
child.id = child.id.replace(oldPattern, newPattern);
}
if (child.childNodes && child.childNodes.length){
modIDs(child, oldPattern, newPattern);
}
}
}

// Just for debug
function showID(e){
var el = e.target || e.srcElement;
if (el.id) alert(el.id);
}

</script>
<body onclick="showID(event);">
<div id="divA">divA_01
<div id="divA_div01">divA_02
<span id="divA_span01">Span 01</span>
</div>
<div id="divA_div03">divA_03
<span id="divA_span02">Span 02</span>
<span id="divA_span03">Span 03</span>
<span id="divA_span04">Span 04</span>
</div>
</div>
<button onclick="cloneEl('divA', 'divB');">Clone it</button>
</body>

--
Rob

Oct 9 '06 #2

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

Similar topics

2
3451
by: Anna | last post by:
Hi all. I am trying to write a stylesheet that will structure the input HTML file in the following way: For each heading of level n it needs to enclose the heading and all its content until the next heading of the same level within the level tag. Example input file:
3
3146
by: Andy Fish | last post by:
Hi, I'm trying to use <xsl:number> to generate a sequence number of all nodes that match a particular pattern, e.g: <xsl:template match="foo"> <xsl:copy> <xsl:attribute name="id"><xsl:number level="any"/></xsl:attribute> </xsl:copy> </xsl:template>
4
612
by: Stephan Tobies | last post by:
Hi everyone, I am looking for a good data structure that could be used to represent families of trees with shared sub-trees and copy-on-write semantics. On a very abstract level, I would like to have an API like this: Let Node be a suitably defined data structure. Then the following functions shall be supported:
7
2250
by: Harrie | last post by:
Hi group, I want to indent existing XML files so they are more readable (at least to me). At this moment I'm looking at the XML files OpenOffice.org's Writer application produces in it's zipped "SXW" format (and they're one line, probably to save space, which I find hard to read). At first I thought I was going to do it with sed/awk or...
4
3007
by: reflex | last post by:
I have to get every node within range or selection? Is it possible? Sry for my engrish :] Ref
1
2758
by: Scott Gifford | last post by:
Hello, I'm working on an providing an iterator interface to a database. The basic thing I'm trying to accomplish is to have my iterator read rows from the database and return constructed objects. My goal here is to abstract away the database part, so that in the future these objects could be constructed from a data source on disk, across...
0
1355
by: empiresolutions | last post by:
Howdy, I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set them as an incremental 1-x values down the page. This is the current code i am using to edit specific nodes. <?php $content = $_POST; $id =...
1
1465
by: empiresolutions | last post by:
Howdy, I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set them as an incremental 1-x values down the page. This is the current code i am using to edit specific nodes. Im trying to use DOM, but it doesn't have...
12
18242
by: blackirish | last post by:
Hi all, I am trying to merge 2 XML files that first of all i need to compare nodes of both files according to 2 attributes in the nodes. If those 3 attributes are equal, i need to replace the existing node with the new one. For example first xml file will be like; <Root> <Node Id ="1" FormId="form1" Value="value1"/> ...
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7563
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.