That's probably because elements are not yet loaded. Make sure to
watch for 'dom:loaded' event on a document. As far as automatically
changing the value - it makes sense to observe 'change' event on a
form:

document.observe('dom:loaded', function() {
  var elements = $$('input[name="event[statistic_attributes][goals][]
[goals]"]');
  $('myForm').observe('change', function() {
    $('result').setValue(elements.inject(0, function(memo, input) {
      return memo += parseFloat($F(input));
    })
  })
})

- kangax

On Apr 24, 4:58 am, Tarscher <[EMAIL PROTECTED]> wrote:
> Many thanks for the replies.
>
> The name of the textboxes of which I want to sum up the values is
> event[statistic_attributes][goals][][goals] . Will the proposed code
> automatically update when I change a value in a textbox?
>
> I put
> <script type="text/javascript">
> //<![CDATA[
> var sum = $$('input[name="event[statistic_attributes][goals][]
> [goals]"]').inject(0, function(memo, input) {
>   memo += parseFloat($F(input));
>   alert(memo);
>   return memo;});
>
> //]]>
> </script>
>
> on top of my page but no effect - the function is not called?
>
> Also, how do I print the variable (memo) to my html?
>
> Sorry for all the beginners questions...
>
> Regards,
> Stijn
>
> On Apr 18, 6:01 pm, kangax <[EMAIL PROTECTED]> wrote:
>
> > The value probably needs to be parsed first : )
>
> > var sum = $$('input[name="amt[]"]').inject(0, function(memo, input) {
> >   memo += parseFloat($F(input));
> >   return memo;
>
> > });
>
> > - kangax
>
> > On Apr 18, 11:43 am, Ken Snyder <[EMAIL PROTECTED]> wrote:
>
> > > Brian Williams wrote:
> > > > i don't believe there is a need to use a class if they share a name
> > > > i.e. amt[]
>
> > > > should be able to use something along the lines of...
>
> > > > $F('amt').each().invoke(function {......})
> > > > ...
>
> > > Right on track.  You'll need $$() and inject()--see 
> > > below.http://prototypejs.org/api/utility/dollar-dollarhttp://prototypejs.or...
>
> > > - Ken Snyder
>
> > > var sum = $$('input[name="amt[]"]').inject(0, function(memo, input) {
> > >   memo += input.value;
> > >   return memo;
>
> > > });
>
> > > or if you define a sum() method for arrays, you can use pluck():
>
> > > Array.prototype.sum = function(){
> > >   for (var i = 0, sum = 0; i < this.length; sum += this[i++]);
> > >   return sum;
>
> > > }
>
> > > var sum = $$('input[name="amt[]"]').pluck('value').sum();
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" 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/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to