Move and copy elements within a document
I just finished my first JavaScript object, and I’m pretty pleased with myself. What this does is make things much easier to move or copy an element from one section of your document to another. See the demo to try it out.
The Code
/*
This script is copyright (c) 2006 Elliot Swan under the
Creative Commons Attribution-ShareAlike 2.5 license:
http://creativecommons.org/licenses/by-sa/2.5/
More information on this script can be found at:
http://www.elliotswan.com/2006/04/12/move-and-copy/
*/
var Move = {
copy : function(e, target) {
var eId = $(e);
var copyE = eId.cloneNode(true);
var cLength = copyE.childNodes.length -1;
copyE.id = e+'-copy';
for(var i = 0; cLength >= i; i++) {
if(copyE.childNodes[i].id) {
var cNode = copyE.childNodes[i];
var firstId = cNode.id;
cNode.id = firstId+'-copy'; }
}
$(target).appendChild(copyE);
},
element: function(e, target, type) {
var eId = $(e);
if(type == 'move') {
$(target).appendChild(eId);
}
else if(type == 'copy') {
this.copy(e, target);
}
}
}
How to use it
After you drop the object into your common JavaScript library, anytime you want to move an element you just call the following method:
Move.element('e', target', 'type');
This has three parameters. The first one, e, should be replaced by the element you wish to copy or move. Replace target with the element you wish to drop e into. The last parameter, type, should be replaced with one of two possible values: move, or copy. A value of move will move the element to the target, whereas a value of copy will copy it (D’oh).
Oh, and one more thing.
Yeah, I listen to too many Steve Jobs keynotes.
But really, there is one more thing. We haven’t discussed what to do with the ids of copied elements. As you should already know, an id should be unique–so they can’t just copy over. So what I’ve done is add -copy to the end of each copied id, in both the main element and all of its children. If the element’s children don’t have ids, none will be added.
Also note that this object uses the Prototype dollar function a few times, so if you aren’t including Prototype you can simply replace all instances of the $ sign with document.getElementById and you’ll be set. Or, just include the following little snippet:
/* Prototype JavaScript framework, version 1.5.0_pre0
* (c) 2005 Sam Stephenson
*
* Prototype is freely distributable under the terms of an MIT-style license.
* For details, see the Prototype web site: http://prototype.conio.net/
*
* The Dollar Function:
/*--------------------------------------------------------------------------*/
function $() {
var results = [], element;
for (var i = 0; i < arguments.length; i++) {
element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
results.push(Element.extend(element));
}
return results.length < 2 ? results[0] : results;
}
And as always, I’d be interested to hear your thoughts. 












Or just include the dollar function into this this little snippet and I’ll digg it. Nice work elliot
Yeah…that’s a good idea, I edited the original post and added it.
Thanks.
Neat. You’re really showing progress.
Awsome, worked like butter!
Write a comment here.
How do you delete or clear?
Wow. Been looking everyone online on how to copy a freakin html element in prototype. Cant find jack shit.
Have to use this hack utility. What a pathetic javascript library
Thank you very much and good work!
Very handy
Hmm It seems no recursion – so better look at http://gist.github.com/4856 and change { id: null } to { id: $(element).id + ‘clone’ }
Created the gertaest articles, you have.
XOz3Fm nvvuvdsebtzb
This is all well and good, anyone know how to copy a StaticNodeList? I can only reference the first item in IE8 without it crashing. I don’t want to believe it’s a bug although my solution (see multiple solutions) works fine in every single browser (even in IE6 + IE7) but Ie8 is throwing tantrums. I only recently beat the living shit out of NodeLists but Static Node List seems to be the cousin that doesnt like playing the dozens.
hehe, 3 years old but still great work
cool, thanks
Great code, but I needed it to recursively update all child node ids and also append a custom name to the ids, rather than ‘-copy’. Enjoy!
Usage:
Move.element(’source_element’,'container’, ‘id_append_variable’, ‘copy’);
Code:
var Move = {
copy: function(e, target, append_name)
{
var eId = $(e);
var copyE = eId.cloneNode(true);
copyE.id = e + append_name;
this.update_ids(copyE, append_name);
$(target).appendChild(copyE);
},
element: function(e, target, append_name, type)
{
var eId = $(e);
if (type == ‘move’)
{
$(target).appendChild(eId);
}
else
if (type == ‘copy’)
{
this.copy(e, target, append_name);
}
},
update_ids: function(e, append_name)
{
var cLength = e.childNodes.length – 1;
for (var i = 0; cLength >= i; i++)
{
if (e.childNodes[i].id)
{
var cNode = e.childNodes[i];
var firstId = cNode.id;
cNode.id = firstId + append_name;
}
if(e.childNodes[i].childNodes.length > 0)
{
this.update_ids(e.childNodes[i], append_name);
}
}
}
}
Good work. Hey your profile picture looks like you’re sulking. What’s up with that? Better if you mouth was up the other way and you’d look less like an moody teenager.
Полезная тема, спасибо.
А мне пригодится.
Great Script
Thanks Elliot
Hope you ALL The Best In Life
This was a great post, thanks for the info.
http://www.niggershitonyourface.com
You can think of your computer’s registry like the mind of your computer. As such, it retailers information not only on every program that your computer has set uped at any given time, it also tends to keep facts from programs that you Previously eradicated. This can be a significant problem for PC owners and is why it’s imperative for computer users to use a free registry cleaner.
When you set up software on your computer, some important information are shopd inside of your computer’s registry. However, when you get rid of or uninstall software, sometimes those records remain within your registry. Maybe the software was imappropriately penned or your computer had a hard time uninstalling the software properly. In either case, the end-result is that you have information in your registry that are no longer needed.
Registry Cleaner Freeware
cleans your computer’s registry. Registry cleaners get rid of outdated and errant registry entries that can cause PC slowdown, error messages and even application crashes. severe registry problems can even result in your computer becoming unbootable. So, by utilizing a registry cleaning tool, you can work to do away with these PC slowdowns and avoid future problems due to a bloated registry.
так хотел посмотреть….а теперь растроен…я ожидал чего-то большего…