Short answer:
var children = parent.selectAll("child").data(newData);
children.exit().remove();
Or, in terms of your code, in the mf() function, you'll want to save
the result of the `data` operator as a local variable, say `var rect`.
Then, after you're done applying operators to the updating selection
(i.e., the `rect` selection), you'll want to access the exiting
selection (`rect.exit()`) and then remove those nodes. You might also
need to handle the entering selection (`rect.enter()`) and append new
nodes if needed.
Narrative answer:
http://mbostock.github.com/d3/tutorial/bar-2.html
Technical answer:
https://github.com/mbostock/d3/wiki/Selections#data
Mike
The exit selection doesn't remove the nodes; that's what the remove operator does. This is discussed at length in the documentation I linked in my previous email.
The code you sent works because you are removing all the elements, then reselecting, and thus you only have entering nodes. However, this is inefficient. It's better if you use the data operator, and then handle the enter, update and exit as three different cases.
In particular, you'll need to change the code so that you store a var as the result of the data operator. Then you can access the enter and exit selections. The standard form is:
var foo = parent.selectAll("foo").data(data);
foo.enter().append("foo")...
foo.exit().remove();
foo... // update here
So, you can't call exit() on the enter() selection, because the exit selection is defined on the result of the data operator only.
Mike