D3 is working on a static html element
<div id="main-container">
<svg id="chart" width="300px" height="500px"></svg>
</div>
But its not working on a dynamically created one.
let newChart = document.createElement("svg");
newChart.id = "chart";
newChart.setAttribute('width', '300px');
newChart.setAttribute('height', '500px');
mainContainer.append(newChart);
D3 Code
this.svg = d3.select(`#chart`);
this.g = this.svg.append("g").attr("transform", "translate(" + MARGIN.left + "," + MARGIN.top + ")");
...
Any idea on why this may be happening?