hphoeksma wrote: > Hi Donny, > > thanks for your reply. This will lead to lots of extra code I guess... > Would there be another way? > > Thanks! > > Henjo
What important is re-bind all code to new element that loaded throught ajax request. One way to do it is put the needed js code in the returned response, so it's get inserted and executed with the new elements. The step 1 in my solution is my practice. If it lead to a lot code rewriting, don't do it for all element, but to the element that got returned from ajax request. > On Apr 16, 5:14 pm, Donny Kurnia <donnykur...@gmail.com> wrote: >> hphoeksma wrote: >>> Hi all, >>> new to jQuery I am having difficulty on the binding concept. >>> Currently using this function to regenerate a full form based on a PHP >>> script. >>> -- >>> $("select").change(function () { >>> $.get('script.php', {'c': c, 'sc': sc }, function(data) { >>> $('form').html(data); >>> }); >>> return false; >>> }) >>> .change(); >>> -- >>> This works, but I'd like to use some next steps in the newly created >>> selectors. >>> I understand I need to (re)bind these selectors, but am lost on how to >>> achieve that. Anyone? >>> Thanks in advance, >>> Henjo >> This is what work for me: >> 1. I write all the js code near the element that need it. In your case, >> I will write the jquery code soon after the </select> tag. >> >> The code will be like this: >> >> <select ....> >> ... >> </select> >> <script type="text/javascript"> >> jquery(function($){ >> $("select").change(function(){ >> .... >> });}); >> >> </script> >> >> 2. When I do ajax that will replace the current DOM element, I will also >> send the code for the new element in the XMLHttpRequest response. >> In your case above, the script.php will return data to .get like this: >> >> ..... >> <select ....> >> ... >> </select> >> <script type="text/javascript"> >> jquery(function($){ >> $("select").change(function(){ >> .... >> });}); >> >> </script> >> ...... >> >> With this approach, the injected data will bring all the needed code for >> it's element. I use this for form, list with pagination, and all other >> XMLHttpRequest. >> >> Writing js code near element that need it, beside maintain the >> unobtrusive, also made debugging it easier. >> >> I hope this will work with your case. >> >> -- >> Donny Kurniahttp://hantulab.blogspot.comhttp://www.plurk.com/user/donnykurnia > -- Donny Kurnia http://hantulab.blogspot.com http://www.plurk.com/user/donnykurnia