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

72
Visualizações
Dismissing alerts in bootstrap 5.1 and Ajax

I am using bootstrap 5.1 alerts to show specific messages upon an Ajax call. Also, I would like to dismiss the alert if needed. For that, bootstrap allows to have a dismiss button.

Here is my php :

<div class="alert alert-warning alert-dismissible" role="alert" style="display:none;" id="message-response-given">
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

And the relevant js for the Ajax call :

document.getElementById("message-response-given").innerHTML = res.message;
$('#message-response-given').fadeIn();

This shows the message BUT not the button after the Ajax call.

I have read the following (bootstrap alerts, same link as above) :

When an alert is dismissed, the element is completely removed from the page structure. If a keyboard user dismisses the alert using the close button, their focus will suddenly be lost and, depending on the browser, reset to the start of the page/document. For this reason, we recommend including additional JavaScript that listens for the closed.bs.alert event and programmatically sets focus() to the most appropriate location in the page. If you’re planning to move focus to a non-interactive element that normally does not receive focus, make sure to add tabindex="-1" to the element.

But I have no clue how to do it.....

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are overwriting the button html when you set:

document.getElementById("message-response-given").innerHTML = res.message;

You need to call append and pass the message as a parameter instead. Since you are using jQuery you can simplify your script to a chained one-liner:

$('#message-response-given').append(res.message).fadeIn();

For example:

$('#message-response-given').append("message").fadeIn();
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">


<div class="alert alert-warning alert-dismissible" role="alert" style="display:none;" id="message-response-given">
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>


<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></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