Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide alert when click on close button instead remove it

In my page, the response from server is displayed using this alert:

    <div id="yes" class="alert alert-success" role="alert" style="display: none;">
      <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
      <strong>Pronto!</strong> cadastro efetuado com sucesso! 
    </div>

Is there any way to when I click in the close button from the alert, it be hide instead removed from page? I try this (following instructions from here), without success:

$('#my-alert').on('close.bs.alert', function () {
  $(this).hide();
})

UPDATE

I also try this:

    $('button.close').on("click", function(){
        $(this).parent().hide();
    });

but still the same problem, when I click in the close button, the alert is removed.

like image 922
Kleber Mota Avatar asked Jul 20 '14 11:07

Kleber Mota


People also ask

How do I add a close button to an alert?

Closing Alerts To close the alert message, add a .alert-dismissible class to the alert container. Then add class="close" and data-dismiss="alert" to a link or a button element (when you click on this the alert box will disappear).

How will you create a bootstrap dismissal alert?

We need to add the class and role fields as alert. Then the element will work as an Alert. Now, we will create a button to dismiss the alert. We can either create an alert inside the element using the data-dismiss field of the button as alert or outside the container a javascript function.

What is bootstrap alert?

Bootstrap Alerts are used to provide an easy way to create predefined alert messages. Alert adds a style to your messages to make it more appealing to the users. There are four classes that are used within <div> element for alerts. .alert-success.

Which bootstrap components is used to provide contextual feedback messages for user actions?

Provide contextual feedback messages for typical user actions with the handful of available and flexible alert messages.


2 Answers

Use the bootstrap close.bs.alert event:

$('.alert').on('close.bs.alert', function (event) {
    event.preventDefault();
    $(this).addClass('hidden');
  });
like image 141
wattry Avatar answered Sep 24 '22 05:09

wattry


I have tried these solutions, but not working for me. You can do it with simply remove "data-dismiss" attribute from your markup and create new function :

    $('.alert').on('click','.close',function(){
        $(this).closest('.alert').slideUp();
   });
like image 22
Upendra Avatar answered Sep 25 '22 05:09

Upendra