clone function

2,953 views
Skip to first unread message

jamiepopkin

unread,
Apr 22, 2011, 10:54:31 AM4/22/11
to d3-js
Hey all.
I'm really excited to have found d3. Thanks to all who made it
possible.

Quick question:
Is it possible to clone an existing svg entity/element? For
example.... with jQuery you would do. $
("#blah").clone().parent().appendto(); . This would give you a
duplicate element as a sibling of the original.

Thanks
Jamie

Yoway Buorn

unread,
Jun 7, 2011, 3:44:32 PM6/7/11
to d3-js
I have the same question. However, I'm not sure that such a method
would fit with the data-driven paradigm. Hence, you may have to use
jQuery or basic DOM manipulation for this particular case.

On Apr 22, 7:54 am, jamiepopkin <popk...@gmail.com> wrote:
> Hey all.
> I'm really excited to have found d3. Thanks to all who made it
> possible.
>
> Quick question:
> Is it possible toclonean existing svg entity/element? For

Mike Bostock

unread,
Jun 7, 2011, 4:16:50 PM6/7/11
to d3...@googlegroups.com
> I have the same question.  However, I'm not sure that such a method
> would fit with the data-driven paradigm.  Hence, you may have to use
> jQuery or basic DOM manipulation for this particular case.

Yeah, you currently have to use the DOM API for this. Something like:

function clone(selector) {
var node = d3.select(selector).node();
return d3.select(node.parentNode.insertBefore(node.cloneNode(true),
node.nextSibling));
}

Then you can say clone("#blah") to select a clone of #blah. You could
made a cloneAll, too:

function cloneAll(selector) {
var nodes = d3.selectAll(selector);
nodes.each(function(d, i) {
nodes[0][i] = this.parentNode.insertBefore(this.cloneNode(true),
this.nextSibling);
});
return nodes;
}

Mike

Jamie Popkin

unread,
Jun 7, 2011, 4:51:41 PM6/7/11
to d3...@googlegroups.com
Cool. Thanks Mike and Yoway.



Reply all
Reply to author
Forward
0 new messages