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

239
Visualizações
How can I use, clear, and re-use div for jQuery status messages?

I have an empty div I use for status messages once my jQuery script executed successfully. I have various jQuery / AJAX calls that use the same div for status messages, however after the first script and first successful population of the div, the div is not reusable anymore, meaning the success message of the second script is not displayed.

How can I populate and then clear a div content in order for it to remain re-useable later?

// populate status div
$('#status').html(response);

// remove success message after 5 secs
setTimeout(function() {
$('#status').fadeOut('fast');
}, 5000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="status"></div>

I tried various other jQuery ways to clear the content, but all have the same result.

  • $('#status').empty()
  • $('#status').val('')
  • $('#status').end()
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Please read documentation on what these jQuery methods does.

.empty() - removes all child elements
.val('') - is intended to be used on input elements only
.end() - End the most recent filtering operation in the current chain and return the set of matched elements to its previous state - does nothing to content itself.

You do not need to clear any content, since fadeOut will hide element. What you want to do is fadeIn that element again and set new content to it

// populate status div
setContent('Initial content');

// remove success message after 5 secs
setTimeout(function() {
    $('#status').fadeOut('fast');
}, 5000);

// show success message again after 10 secs
setTimeout(function() {
    setContent('Repeated content');
}, 10000);

function setContent(content) {
  $('#status')
      .html(content)
      .fadeIn('fast');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="status"></div>

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