Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
Change text in SVG image with JS

Currently, I am working with SVG image and JS. I want to change the text content in the SVG image. Actually, I already did it, but sometimes it's work, sometimes not. I use the function below:

function injectSVG(params) {

  var svgChart = document.getElementById("item-svg-chart").contentDocument;
  var text = svgChart.querySelectorAll('text.text-inject');
  for (var i = 0; i < text.length; i++) {
    text[i].textContent = params[i].toString().replace("*", "\xB0");
  }
}

with params content new text will be injected.

The code I used to show svg on html:

<div class="img-fluid">
  <object alt="svg-chart" id="item-svg-chart" data="{{ url_for('static', filename='img/rf-weldneck-flange.svg') }}"></object>
</div>

SVG images will change with the code below (I also query it):

  $('#third-select-card').on("change", function(e){
    var link = selectChange();
    $("#item-svg-chart").attr('data', link);
    var svgChart = document.getElementById("item-svg-chart");
    console.log(svgChart);
    var svgChart_1 = document.getElementById("item-svg-chart").contentDocument;
    console.log(svgChart_1);
  })

with svgChart show svg A (for example), svgChart_1 show contentDocument of svg A and link is the link to SVG image. But when query the svgChart and svgChart_1 I saw they were not the same svg contenDocument but it show the same image on my web (see attached images):

Query svgChart

Query svgChart_1

I don't know why it show the same image on web, but contentDocument is different. I think it's maybe the reason why I can not inject new text to svg.

Can someone help me explain why contentDocument is different ? And if the code I posted above is meaningless, please tell me there are anyway to change text content in svg image ?. This issue got me 2 days for it.

Thanks for help!

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda