I am trying to update my web page content when the user clicks on a button using D3. Unfortunately I can see that onclick new data is being displayed, but the old data is not fo
You need to select the elements that actually exist and do that only once:
var sel = svg.selectAll("rect") .data(data); sel.exit().remove(); sel.enter().append("rect") // etc