On Apr 23, 2009, at 2:05 PM, katz wrote:
> > Greetings. > I need to learn how to achieve to dynamically copy form field values > using Prototype or possibly Ruby. > > I want to achieve something like this: > http://websitebuildersresource.com/2009/02/05/copy-form-input-data-to-another-input-field-with-jquery/ > > http://websitebuildersresource.com/demos/jquery-copy-form-data/ > > Sorry for posting a jQuery example. > I'm fairly new to Prototype, Scriptaculous and RJS. My goal is to > achieve similar result using the default libraries of Rails. > Although I am aware of jQuery noConflict() and and all those > techniques > to make jquery work well with other libraries, I just don't want to > use > it for now. > > There aren't too many decent examples on how to copy form values for > prototype. > > My question is: > How do you use that $F function and form.serialize to copy form field > values to other fields? > Also event observers seem to fail to work if defined on head tag. Am I > right? If you define the observer before the element it's observing is loaded into the DOM, then it will fail. Try wrapping your element observer in a general "the page has loaded" observer, either one of these will do: document.observe('dom:loaded',function(){ //your code here }); or (more IE-friendly ATM) Event.observe(window,'load',function(){ //your code here }); > > > http://www.prototypejs.org/api/event/observe > > Examples aren't working for me. > > So I already have something like this: > <input id="item_weight" > type="text" value="0" size="10" name="item[weight]"/> > > > <input id="item__copy_weight" > type="text" value="0" size="10" name="item[copy_weight]"/> > > > <input type="button" id="button3" value="Same as above" /> > > <script type="text/javascript"> > > $('button3').observe('click', function(e) { > var form = Event.findElement(e, 'form'); > > var input = form.down('input[name="item[weight]"]'); > > alert($F(input)); > }); > </script> > What's missing is the code to make the value of item[weight] same as > [copy_weight] when the button is clicked. > Help would be greatly appreciated. > > The easiest way to do this would be to give your form elements IDs to match their names. Then, you can simply do this: $('button_3').observe('click',function(){ $('item_copy_weight').setValue($F('item_weight')); }); If you still need to do the crawl up then down thing to get the non- identified elements, you can use previous() and next() to walk between neighbors, as long as they are in the same parent container. Walter --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---
