How to show message box and close it when clicked an element using jQuery?
It is something like to show a success message when clicked a button.
jQuery - Show simple message box when clicked?
8.2k views Asked by Louis55 At
        	3
        	
        There are 3 answers
0
                
                        
                            
                        
                        
                            On
                            
                            
                                                    
                    
                According to your requirements, I think you want a toast sort of message box. You can do it like this:
$(function() {
  function myFunction() {
        var x = document.getElementById("snackbar")
        x.className = "show";
        setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
  }
  $("#showToast").on('click', function(e) {
    myFunction();
  })
});
#snackbar {
    visibility: hidden;
    min-width: 250px;
    margin-left: -125px;
    background-color: #333;
    color: #fff;
    text-align: center;
    border-radius: 2px;
    padding: 16px;
    position: fixed;
    z-index: 1;
    left: 50%;
    bottom: 30px;
    font-size: 17px;
}
#snackbar.show {
    visibility: visible;
    -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
    animation: fadein 0.5s, fadeout 0.5s 2.5s;
}
@-webkit-keyframes fadein {
    from {bottom: 0; opacity: 0;} 
    to {bottom: 30px; opacity: 1;}
}
@keyframes fadein {
    from {bottom: 0; opacity: 0;}
    to {bottom: 30px; opacity: 1;}
}
@-webkit-keyframes fadeout {
    from {bottom: 30px; opacity: 1;} 
    to {bottom: 0; opacity: 0;}
}
@keyframes fadeout {
    from {bottom: 30px; opacity: 1;}
    to {bottom: 0; opacity: 0;}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Toast</h2>
<p>Toasts are often used as a tooltips/popups to show a message at the bottom of the screen.</p>
<p>Click on the button to show the snackbar. It will disappear after 3 seconds.</p>
<button id="showToast">Show Toast</button>
<div id="snackbar">Some text some message..</div>
Hope this helps!
HTML, CSS, JS:
(The message box will be shown on top of the cursor and aligned at the center.)
It is recommended to append the message(
div.msg) tobody.