Jquery Event Click After Ajax Post

686 Views Asked by At

I'm with a problem in my code, when I do a ajax post, the click event stop working, here is my code:

$("#register_button").click(function(){
   if(condittion_1){
      code...
   }
   else{
     my ajax post...
   }
}

The problem is, after the ajax post, the click button event stop working. I made a test, I removed the ajax from the Else, and put out it, the ajax post was working and the button click event was normal, but when I put it in else, the click event stop working...Please, I need some help =(

1

There are 1 best solutions below

8
Trung Duong On BEST ANSWER

You not post all your detail code so I could only take a guess. I think in success function of ajax post, a part of html on your page, include button #register_button, will be updated. In that case, inside success function of ajax post, you need to re-bind click event for button #register_button.
You could try to update your code as following:

function handleClickEvent() {
    $("#register_button").click(function(){
       if(condittion_1){
          code...
       }
       else{
         $.post( "example.php", function() {
            // other code of success function 
            ....
            // call function to re-bind click event
            handleClickEvent();

         })
       }
    }
}