Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show/hide a specific alert with twitter bootstrap?

Here's an example of an alert I'm using:

<div class="alert alert-error" id="passwordsNoMatchRegister">   <span>     <p>Looks like the passwords you entered don't match!</p>   </span> </div> 

I know that $(".alert").show() and $(".alert").hide() will show/hide all the elements of the .alert class. However, I cannot figure out how to hide a specific alert, given its id.

I want to avoid using .alert("close"), since that permanently removes the alert, and I need to be able to recall it.

like image 504
royhowie Avatar asked Jun 28 '13 05:06

royhowie


People also ask

Which bootstrap 4 class can be used to create successful alert?

Bootstrap 4 provides an easy way to create predefined alert messages. Alerts are created with the . alert class, followed by one of the eight contextual classes .

How do I make bootstrap alerts automatically disappear?

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 do I show and hide a bootstrap alert?

show() and $(". alert"). hide() will show/hide all the elements of the . alert class.


2 Answers

You need to use an id selector:

 //show  $('#passwordsNoMatchRegister').show();  //hide  $('#passwordsNoMatchRegister').hide(); 

# is an id selector and passwordsNoMatchRegister is the id of the div.

like image 113
bipen Avatar answered Oct 11 '22 00:10

bipen


Add the "collapse" class to the alert div and the alert will be "collapsed" (hidden) by default. You can still call it using "show"

<div class="alert alert-error collapse" role="alert" id="passwordsNoMatchRegister">   <span>   <p>Looks like the passwords you entered don't match!</p>   </span> </div> 
like image 38
Mike Avatar answered Oct 11 '22 01:10

Mike