Modal window does not work correctly by clicking YES

Asked

Viewed 59 times

0

I’m having some difficulty with the following Jquery code:

JQUERY

$(document).ready(function(){
     $('#modalLogout').click(function(){
          $('#yes').click(function(){

              function time()
              {
                 $(location).attr('href',"destroy.php");
              }

              /*set 1s after ckick in yes, after that redirect to index*/           
              setTimeout(function(){time()},1000);
              /*table fadeout effect*/  
              $(".row").hide("slow");
        }); 
     });
});

What happens is that when opening a modal confirmation window if you are sure you want to continue or end the session, when I click YES it does nothing the first time, I have to open the window again and click YES for the time() function to be executed. But when I click on NO it works correctly, that is, it closes the window and stays in the login. How could I change this strange behavior and improve the code?

2 answers

4


You only set what needs to be done at the YES click when you click #modalLogout. I did not understand the intention of this "outside" part of the code, so it would be enough to define the YES click separately:

$(document).ready(function(){
     $('#yes').click(function(){

          function time()
          {
             $(location).attr('href',"destroy.php");
          }

          /*set 1s after ckick in yes, after that redirect to index*/           
          setTimeout(time, 1000);
          /*table fadeout effect*/  
          $(".row").hide("slow");
    }); 
});

Notice I also simplified your call to setTimeout.

  • Solved the problem, now the behavior is correct!

1

If your intention was to assign the click event to the element #yes child of the element #modalLogout then the change that must be made in your code is as follows:

function time() {
    $(location).attr('href',"destroy.php");
}

$(document).ready(function() {
    $('#modalLogout #yes').click(function(){         
        setTimeout(time, 1000);

        $(".row").hide("slow");
    });
});
  • The top code worked as I wanted, but yours goes more against what I was programming and works as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.