I\'m using d3 to append some elements on enter() and then update them later. However, the next time i try to select those elements the selection is much larger than the orig
Late to the party, but at least in d3
version 4, selection.selectAll()
can take a function whose result is an array that contains the new elements to select based on the selected element in the previous selection:
var parent = d3.selectAll(".myParentClass");
var children = parent
//Convert selection to selection representing the children
.selectAll(function() { return this.childNodes; })
//Apply filter to children
.filter('g')
;
The primary benefit of this approach compared to the previous answers are that the selection.data()
function will still work correctly. The previously proposed methods, which assign results from a new, separate d3.select()
call, do not allow this.