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

237
Views
¿Cómo (dinámicamente) agregar elementos a un <svg> con javascript?

Estoy haciendo un gráfico usando un elemento <svg> . Algunos datos deben agregarse dinámicamente con Javascript. Obtuve una versión simplificada de mi problema aquí en JsFiddle .

Lo que estoy haciendo es obtener un elemento por su id . Luego creo el nuevo elemento de texto y establezco los attributes y el innerHTML . Por último, lo estoy agregando a <svg> .

Lo extraño es que cuando inspecciono el elemento svg en la consola, parece que el elemento que se agrega con Javascript está allí. Sin embargo, no es visible en la pantalla:

ingrese la descripción de la imagen aquí

Mi código:

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!