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

207
Visualizações
innerHTML disappearing quickly from div in jQuery load file

innerHTML appears then quickly disappears in under a second, div in a jquery load file.

main.js

$(document).ready(function () {
    $('#page1').click(function () {
        $('#page-content-wrapper').load('page1.html');
        document.getElementById("changeme").innerHTML = "Paragraph changed!";
    })
});

index.html

<!DOCTYPE html>
<html>
<head>
    <script src="js/site.js"></script>
    <meta charset="utf-8" />
    <title>TimeApp</title>
</head>
<body>
    <a id="page1">Page 1</span></a> <a id="page2">Page 2</span></a>
    <div id="page-content-wrapper">
        <!-- Page content is loaded here -->
    </div>
</body>
</html>

page1.html

<div id="changeme"></div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue is because load() makes an AJAX request to retrieve the content from page1.html. While this is happening you update the innerHTML of the element within the #page-content-wrapper. When the AJAX request completes, the content of page1.html overwrites the existing content - which you just updated.

To fix the problem put the line which updates the text of the element in the callback of load(), so that it's only ever executed after the AJAX request:

jQuery($ => {
  $('#page1').click(function() {
    $('#page-content-wrapper').load('page1.html', () => {
      $("#changeme").text("Paragraph changed!");
    });
  });
});

Better still, put the content you need in to page1.html directly, to avoid this unnecessary code.

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