Search code examples
javascriptjquerybpopup

How to hide bpopup jquery?


How can i hide the bpopup jquery using a button? i have an ajax response that if the data return error has failed bpopup will be called. then inside my bpopup i have a "Got it!" button when the user click it, it will just close the bpopup

 $.ajax({
     url: 'my_url',
     type: "POST",
     success: function(data){
         if(data.error == 'failed){
            $('#popup').bPopup({
               modalClose: false,
               opacity: 0.6,
               positionStyle: 'fixed' 
            });

            //then my bpopup has a button "Got it!"
            $('.gotit').click(function(e) {
               //Code the will hide the bpopup.
            }
         }
     }
 })

i have tried $('#popup).hide(); but it does not completely closed the bpopup.

BTW here's my popup html.

 <div id="popup" style="display: none; min-width: 350px;">
     <span class="button b-close"><span>X</span></span>
     <div class="col-lg-12">
          <p><span class="ui-icon ui-icon-alert"></span>&nbsp;&nbsp;The Code does not match the information provided.</p>
          <button class="btn btn-primary btn-sm pull-right gotit">Got it</button>
     </div>
 </div>

Solution

  • First of all

    if(data.error == 'failed){ here ' is missed so add it and make it:-

    if(data.error == 'failed'){

    Closing pop-up can do it in two ways

    1.Hide the pop-up directly.

    $('.gotit').click(function() {
    
      $(this).closest('#popup').hide();//hidepop-up directly
    
      // also you can use $(this).parent().parent('#popup').hide();
    });
    

    Example:-

    $('.gotit').click(function() {
    
      $(this).closest('#popup').hide();//hidepop-up directly
      
      // also you can use $(this).parent().parent('#popup').hide();
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="popup" style="display: block; min-width: 350px;"><!--changed disply:block to show you that how it will work -->
         <span class="button b-close"><span>X</span></span>
         <div class="col-lg-12">
              <p><span class="ui-icon ui-icon-alert"></span>&nbsp;&nbsp;The Code does not match the information provided.</p>
              <button class="btn btn-primary btn-sm pull-right gotit">Got it</button>
         </div>
     </div>

    2.Trigger close button click event of pop-up (if close button code is already written and working)

    $('.gotit').click(function() {
      $('.b-close').click();
    });
    

    Example:-

    $('.b-close').click(function() { //if your close button code is alwready written
      $('#popup').hide();
    });
    
    $('.gotit').click(function(){
      $('.b-close').click(); // trigger close button clik event
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="popup" style="display: block; min-width: 350px;"><!--changed disply:block to show you that how it will work -->
         <span class="button b-close"><span>X</span></span>
         <div class="col-lg-12">
              <p><span class="ui-icon ui-icon-alert"></span>&nbsp;&nbsp;The Code does not match the information provided.</p>
              <button class="btn btn-primary btn-sm pull-right gotit">Got it</button>
         </div>
     </div>