avoiding duplicate ids with cloneNode

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 -->
// ex 1:
var confirm_tpl = getElementByID("confirm_tpl");
var confirm = confirm_tpl.cloneNode(true);
// ex 2:
var remove_div = getElementsByClassName("commands",getElementByID("tpl_holder"),"div")[0].cloneNode(true);

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David S.Commented:
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?
drew22Author Commented:
>>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
David S.Commented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
drew22Author Commented:
right, the clone isn't yet part of the document. forgot about that
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.