ajax - Run jquery on newly created content without duplicating code -


when document ready, want run javascript function on elements class "test".

$(document).ready(function {     $('.test').dosomething(); }); 

imagine dynamically load contents div jquery/ajax. content may contain elements class test. how make sure dosomething called on them well. in other words, want function called on elements class test appear, regardless of whether there in first place or loaded later dynamically. of course, write

$('.test').dosomething(); 

inside ajax callback well, i'm hoping avoid writing same code in every ajax callback. possible? make sense?

an expansion of comment above:

jquery has couple methods let register global handlers ajax requests http://api.jquery.com/ajaxsuccess/ . use , call dosomething every time make successful request, filtering .test elements have had method called.

there lot of ways accomplish "filtering", absolute simplest way add class every .test element after fire method.

$(document).ajaxsuccess(function () {   $('.test:not(.added)').addclass('added').dosomething(); }); 

this simultaneously filter out ones you've added.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -