-1

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.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132

2 Answers2

1

Find both <div> and <svg> by their IDs with document.getElementById() and use Node.appendChild() API to add one to another.


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

<script>
  const div = document.getElementById('info');
  const svg = document.getElementById('BarChart');
  div.appendChild(svg); // <- this will remove svg from it's previous location in DOM and append as the last element inside a div
</script>
Vadim
  • 8,701
  • 4
  • 43
  • 50
0

Using appendChild:

const BarChart = document.querySelector('#BarChart');
const info = document.querySelector('#info');
info.appendChild(BarChart);
Mr-montasir
  • 132
  • 1
  • 1
  • 8