If you pass an existing DOM element to append or insert , the element (and its children) will be moved from their current location in the DOM hierarchy to where you just pasted them.
var stuffToBeWrapped = d3.selectAll(".stuff"); stuffToBeWrapped.each(function() { d3.select( this.parentNode ).insert("g", function(){return this;} )
This is a bit messy because d3 expects you to always use functions to determine which item to insert or insert earlier, and that this is not necessary when we do all this in each statement. But he has to do his job!
(Or use the jQuery method if you already have a library on your page. JQuery usually has no problems managing SVG elements, they just can't create them!)
source share