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

182
Visualizações
Unable to update element ID of HTML text using Javascript Jquery

Using JS and AJAX, I am loading a template file on my Index.html page. Once the template is loaded, I then want to apply changes to the DOM.

  • The template is successfully loading on the index.html page.
  • The JS is FAILING to update the DOM elements.

What am I doing wrong?

I have 2 html pages.

  1. index.html
  2. page-banner-area.html

index.html

<div id="page-banner-area"></div> 

<script>
$(function(){
  $("#page-banner-area").load("assets/static_html/page-banner-area.html");
    $("#title").text('Join a Community Group in your area.');
    $("#keypoint-1").text('We are against mandatory vaccines &amp; passports.');
    $("#keypoint-2").text('We do not stand for corruption &amp; censorship.');
    $("#keypoint-3").text('We believe in freedom!');
});
</script>

page-banner-area.html

<div class="p-2 flex-grow-1">
   <h3><span id="title"></span></h3>
      <span id="keypoint-1"></span><br />
      <span id="keypoint-2"></span><br />
      <span id="keypoint-3"></span>
</div>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is that the load() method is asynchronous, you need to do your changes within a callback method in order for them to be properly applied. See the JQuery docs for more information https://api.jquery.com/load/

As for a solution, you should be doing this instead:

$(function(){
  $("#page-banner-area").load("assets/static_html/page-banner-area.html", function() {
    $("#title").text('Join a Community Group in your area.');
    $("#keypoint-1").text('We are against mandatory vaccines &amp; passports.');
    $("#keypoint-2").text('We do not stand for corruption &amp; censorship.');
    $("#keypoint-3").text('We believe in freedom!');
  });
});
about 4 years ago · Juan Pablo Isaza Relatório

0

I thinks this can solve your problem!

<div id="page-banner-area"></div> 

<script>
$(() => {
  $("#page-banner-area")
    .load("assets/static_html/page-banner-area.html", () => {
      $("#title").text('Join a Community Group in your area.');
      $("#keypoint-1").text('We are against mandatory vaccines &amp; passports.');
      $("#keypoint-2").text('We do not stand for corruption &amp; censorship.');
      $("#keypoint-3").text('We believe in freedom!');
    });
});
</script>

about 4 years ago · Juan Pablo Isaza Relatório
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