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.
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 .
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).
show() and $(". alert"). hide() will show/hide all the elements of the . alert class.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With