Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #15778
| From | Mike Winter <usenet@michael-winter.me.invalid> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: How to do calculation with dynamic form field names? |
| References | <2f50ffc9-e54f-493b-aae9-378dac92b6c1@z8g2000yql.googlegroups.com> <8un2s.290330$Lp7.53018@fx08.am4> <c40ba0e0-3f3a-4333-b9da-60949a55c236@n9g2000yqn.googlegroups.com> |
| Message-ID | <Tzr2s.523194$6h3.7439@fx25.am4> (permalink) |
| Organization | virginmedia.com |
| Date | 2012-09-07 20:05 +0100 |
On 07/09/2012 16:03, justaguy wrote:
> On Sep 7, 10:26 am, Mike Winter <use...@michael-winter.me.invalid>
> wrote:
>> On 07/09/2012 05:07, justaguy wrote:
>>
>> [snip]
>>
>>> <form>
>>> <input type="text" id="item1">, <input type="text" id="qty1">, <input
>>> type="text" id="price1">, fieldX1 t1
[snip]
> Strange. After I changed the script a bit to get subtotal value for
> each row,
>
> var i = 0, t=0,
Here, i should've been initialised to 1: the first row has a "qty1", not
a "qty0". This was an oversight on my part.
> element;
> while ((element = forms[0].elements['qty' + i])) {
As Gene pointed out, you cannot just use "forms" as-is. With the code I
originally posted. the simple adaptation would be to declare and
initialise form:
var form = document.forms[0];
I didn't include it as that might not be the best way to obtain that
reference. If your function is called from the form element's submit or
the button's click event handler, then:
<form ... onsubmit="return calculateTotal(this.form);">
function calculateTotal(form) {
// ...
would be better. Regardless, using a single reference has the advantage
of avoiding repeated property references and, in my opinion at least,
improving readability.
> var subtotal = forms[0].elements['qty' + i] *
> forms[0].elements['price' + i];
> forms[0].elements['total' + i].value = subtotal;
> t = t + parseFloat(subtotal);
t += subtotal;
The subtotal variable will already be a floating-point number as the
multiplication operator attempts to type-convert strings to numbers
automatically. There's no need to call parseFloat here. If you're
concerned with values not being numbers, you need to be validating those
values.
[snip]
> Am I missing something here?
Hopefully that's cleared up a few things.
Kind regards,
--
Mike Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: How to do calculation with dynamic form field names? Mike Winter <usenet@michael-winter.me.invalid> - 2012-09-07 15:26 +0100
Re: How to do calculation with dynamic form field names? justaguy <lichunshen84@gmail.com> - 2012-09-07 08:03 -0700
Re: How to do calculation with dynamic form field names? Gene Wirchenko <genew@ocis.net> - 2012-09-07 10:05 -0700
Re: How to do calculation with dynamic form field names? justaguy <lichunshen84@gmail.com> - 2012-09-07 11:39 -0700
Re: How to do calculation with dynamic form field names? SAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> - 2012-09-07 21:56 +0200
Re: How to do calculation with dynamic form field names? Mike Winter <usenet@michael-winter.me.invalid> - 2012-09-07 20:05 +0100
Re: How to do calculation with dynamic form field names? justaguy <lichunshen84@gmail.com> - 2012-09-07 12:36 -0700
Re: How to do calculation with dynamic form field names? justaguy <lichunshen84@gmail.com> - 2012-09-07 12:51 -0700
Re: How to do calculation with dynamic form field names? Mike Winter <usenet@michael-winter.me.invalid> - 2012-09-07 20:57 +0100
csiph-web