Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
How to (dynamically) add elements to a <svg> with javascript?

I am making a graph using an <svg> element. Some data needs to be added dynamically with Javascript. I got a simplified version of my problem here on JsFiddle.

What I am doing is getting an element by it's id. Then I create the new text element and set the attributes and innerHTML. Lastly, I am appending it to the <svg>.

The strange thing is that when I inspect the svg element in the console, it appears as if the element that is added with Javascript is there. However, it is not visible on the screen:

enter image description here

My code:

HTML

<svg>
    <g id="x_label">
        <text x="20" y="20">asd</text>
    </g>
</svg>

Javascript:

window.onload = () => {
  printChart()
}

function printChart() {
  let xLabel = document.getElementById('x_label')
  let newLabel = document.createElement("text")
  newLabel.setAttribute("x", "20")
  newLabel.setAttribute("y", "40");
  newLabel.innerHTML = "new label"
  xLabel.appendChild(newLabel)
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!