Hi all, I have multiple textboxes containing numbers. I want to add up all the numbers and show the sum. Can I select the textboxes by class and sum the content? This also has to happen realtime: when a number is changed ina textbox the sum should also change. can this be done? regards, Stijn --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 {......}) something like that at any rate, one of the more experienced js/pr programmers can do a better job of filling in the details than I can On Fri, Apr 18, 2008 at 11:25 AM, Tarscher <tarscher-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi all, > > I have multiple textboxes containing numbers. I want to add up all the > numbers and show the sum. Can I select the textboxes by class and sum > the content? > > This also has to happen realtime: when a number is changed ina textbox > the sum should also change. > > can this be done? > > regards, > Stijn > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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-dollar http://prototypejs.org/api/enumerable/inject - 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 <kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.org/api/enumerable/inject > > - 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 <tarsc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---