steve dp
2005-Oct-14 08:28 UTC
using observe_field on fields created dynamically through ajax
I''m trying to use observe_field on a field w/ id ''chapters''. Problem is, chapters gets re-created through another ajax call on the page. The id is the same, still chapters, but its not really the "original" one. Once it has been re-created the observe_field no longer works. It works fine before it gets re-created. I figured this makes sense with an event based observe_field so I tried making it a time-based check using :frequency => 1, figuring this would use getElementById and should work fine even if it gets recreated, but alas it did not ever fire off the event when a value changed. : / Any suggestions? Other than change the prototype library myself? - steve
steve dp
2005-Oct-14 08:44 UTC
Re: using observe_field on fields created dynamically through ajax
steve dp <oksteev@...> writes:> > I''m trying to use observe_field on a field w/ id ''chapters''. Problem is, > chapters gets re-created through another ajax call on the page. The id is the > same, still chapters, but its not really the "original" one. Once it has been > re-created the observe_field no longer works. It works fine before it gets > re-created. > > I figured this makes sense with an event based observe_field so I tried making > it a time-based check using :frequency => 1, figuring this would use > getElementById and should work fine even if it gets recreated, but alas it did > not ever fire off the event when a value changed. : / > > Any suggestions? Other than change the prototype library myself? > > - steve >I answered my own question. I included the observe_field helper call in the partial which is used to update the div. So each time the control is recreated so is the observe_field call.
steve dp
2005-Oct-14 09:11 UTC
Re: using observe_field on fields created dynamically through ajax
This actually seems like something that should be changed. It seems pretty common to have observers on elements which themselves will be updated in the lifetime of a page. To have to refresh the observer script as well seems wrong. It seems like a better approach to change the observers to resolve the element id every time they try to access it instead of doing it once at the beginning and storing the reference. I''m sure the time you save by only doing it once is very minimal. Just my thoughts.. - steve