knpwrs
knpwrs

Reputation: 16456

D3 remove parents of elements returned by exit

The following code will remove old paths:

var join = svg.selectAll('path').data(data, function (d) {
  return d.label;
});
join.exit().remove();

Each path element that I am removing is contained within a g element containing everything I want to remove.

<g class="item">
  <text class="line-text">Label</text>
  <path class="line" d="..." style="stroke: #2ca02c;"></path>
</g>

So what I really want to do here is remove the g element, but calling .remove() only removes the path element. How can I remove the entire g element?

Upvotes: 2

Views: 946

Answers (1)

Lars Kotthoff
Lars Kotthoff

Reputation: 109232

The proper way to do this would be to bind the data to the g elements so that you operate on them. The quick and dirty way to do what you're asking for would be the following.

join.exit().each(function() { d3.select(this.parentNode).remove(); });

Upvotes: 2

Related Questions