Delete and redraw svg D3

Delete and redraw svg in D3 JS

by sundaramkumar

HTML

<svg width="100" height="100" style="border:1px solid red"></svg>
<button id="update">Update</id>

JavaScript

/* var svg = d3.selectAll("body").append("svg"); */
var svg = d3.select("svg");
svg.append("circle").attr("cx",20).attr("cy",20).attr("r",20).style("fill","red");

d3.select("#update")
    .on("click",update);

function update() {
    svg.remove();
     svg = d3.selectAll("body").append("svg")
     .style('border','1px solid green')
     .attr('width',100)
     .attr('height',100);
     
     
    svg.append("circle")    
        .attr("cx",40)
        .attr("cy",40)
        .attr("r",20)        
        .style("fill","blue"); 
}