this makes an explicit seb answer. where you set up the click handler, put this:
.on("click", function(d) { if (d.parent && d.parent.children){ console.log('removing ' + d.name); var nodeToDelete = _.where(d.parent.children, {name: d.name}); if (nodeToDelete){ d.parent.children = _.without(d.parent.children, nodeToDelete[0]); } } });
which should get what you want. be sure to call any method that draws a tree from the source data, which is now modified. note: I use the underscore library to use _.where and _.without, although you could do it with pure js or with another library. some of the checks are for convenience and to prevent root node removal, for example.
ps since you probably want to keep your expand / collapse behavior, then just put the conditional expression in the callback to check the modifier key to indicate the removal of the node using, for example, d3.event.altKey.
source share