Solved

avoiding duplicate ids with cloneNode

Posted on 2008-10-07
4
2,320 Views
Last Modified: 2012-06-27
I like to use cloneNode instead of dynamically building up html via DOM methods. However, grabbing the element via getElementBy Id and then clone it, you have duplicate ids.

I usually use the technique in ex 2, but I was wondering if there is anything wrong with the way in ex 1 where I grab the element by id, remove the id attribute, clone the element, set the id attribute back again

<!--  this div just holds templates for cloning -->
<div id="tpl_holder" style="display:none">
 
   <div "confirm_tpl">
   <-- bunch of html here -->
   </div>
 
</div>
 
// ex 1:
var confirm_tpl = getElementByID("confirm_tpl");
confirm_tpl.removeAtrribute("id");
var confirm = confirm_tpl.cloneNode(true);
confirm_tpl.setAtrribute("id","confirm_tpl");
 
 
// ex 2:
var remove_div = getElementsByClassName("commands",getElementByID("tpl_holder"),"div")[0].cloneNode(true);

Open in new window

0
Comment
Question by:drew22
[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 42

Expert Comment

by:David S.
ID: 22668023
Why remove the ID of the element before cloning it? Why not just change the ID of the clone before adding it as a child of an element in the document?
0
 
LVL 1

Author Comment

by:drew22
ID: 22671075
>>Why not just change the ID of the clone before adding it as a child
because before you change the id of the clone you will have 2 elements with the same id and element ids must be unique
0
 
LVL 42

Accepted Solution

by:
David S. earned 500 total points
ID: 22671543
Yes, but the problem is only if both elements are part of the document. If only one of them is, then it doesn't matter.
0
 
LVL 1

Author Comment

by:drew22
ID: 22671632
right, the clone isn't yet part of the document. forgot about that
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Date on a table 16 36
Session on Html 8 42
Table header must be on top 2 31
ExcelWebAddIn Connect to SQL database (Javascript or VB.net) 7 27
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

726 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