Search code examples
javascripthtmlsvgtags

How to add an SVG as a child of a DIV?


In my HTML code, I have something like below:

<div id="info">...</div>
<svg id="BarChart" class="chart" width="500" height="300" transform="translate(-110, 0) rotate(-90)">...</svg> 

How can I make this SVG a child of the DIV using JavaScript? I want to have a paragraph in div and then my chart.


Solution

  • Using appendChild:

    const BarChart = document.querySelector('#BarChart');
    const info = document.querySelector('#info');
    info.appendChild(BarChart);