Hi,
For cloning, there's always Element#clone[1]. That clones the node
using the underlying DOM cloneNode function, and also handles anything
Prototype might have stored on the node.
> Apart from cloning an element I also needed to move an element from
> one div to another and I came up with this function...
> [snip]
> I don't like the switch part very much and tried to use the position
> variable directly but that somehow didn't work...anyone knows why?
> [snip]
> newParent.insert({position: element});
Because that creates a property called "position" on the options
object, exactly the way your earlier code
> newParent.insert({top: element});
...creates a property called "top" on the options object. The left-
hand side of a property initializer (and for that matter an
assignment) is used as the name of the property (or variable) to
create, not as a variable _containing_ the name of the property/
variable.
It is possible to do what you want, though:
Element.addMethods({
appendTo: function(element, newParent, position) {
var options;
element = $(element);
newParent = $(newParent);
options = {};
options[position] = element;
newParent.insert(options);
return element;
}});
...because the bracketed notation for property access uses strings for
property names, and lets you get the property name string from a
variable.
[1]
http://api.prototypejs.org/dom/element/clone/
HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com
On Jun 20, 11:48 pm, David Behler <
d.beh...@gmail.com> wrote:
> Hi guys,
>
> while looking for a way to clone an element, I stumbled upon this
> thread from 2007:
>
>
http://groups.google.com/group/prototype-core/browse_thread/thread/9e...