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

171
Visualizações
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 à 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