Search code examples
jqueryajaxauthenticationrefreshelement

jQuery.ajax new element check


im trying to make loging to web page using ajax. I have this:

    $.ajax({
       type: "POST",
       url: "ajax/login.php",
       data: dataString,               
       success: function(answer) {
           $('#loging').html(answer);           
       }      
    });

It works fine, BUT: In answer variable is something like this:

    Hi user, <span id="logout_button">Logout</span>

I can see button "Logout", but my browser dont know here is any span id="logout_button". I have code for logout binded with #logout_button but it dosnt work, because my browser knows nothing about this button. When I refresh page, logout works fine.

So, is there any way/function how to say to jQuery "hi buddy, please check for the new elements on document"?


Solution

  • Yes, you can use .delegate() instead of .bind()

    $('body').delegate('#logout_button', 'click', function() {
        //here the code
    });