Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

252
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda