Using live function instead of bind with jQuery

Hi all,

Sometimes a big part of an application uses AJAX to get content. When getting new content we get new events, already declared in the webapp. For example:

$(function(){  
    $('a.class1').bind('click', function(){  
        // code here  
    });  
});

If the new content (via AJAX) has a link with class1 class, it won’t call this javascript code, because the click event was previously declared.

So, the solution is use live function instead of bind. When using the live function, the new inserted content will be updated to use the declared javascript events. So it’s better to use live.

$(function(){  
    $('a.class1').live('click', function(){  
        // code here  
    });  
});

Thanks!

Published by

Junior Grossi

senior software engineer & stutterer conference speaker. happy husband & dad. maintains Corcel PHP, elePHPant.me and PHPMG. team lead @ Glofox

Leave a Reply

Your email address will not be published. Required fields are marked *