Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #15769
| 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> |
| Message-ID | <8un2s.290330$Lp7.53018@fx08.am4> (permalink) |
| Organization | virginmedia.com |
| Date | 2012-09-07 15:26 +0100 |
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]
> a) t{n} = subtotal field.
> b) number of rows is unknown, it could be 1 to 30.
>
> What I'd like to do is: calculate subtotal and total.
> each subtotal = qty{n} * parseFloat(price{n})
>
> My attempt is to loop through the entire form, I'm able to retrieve
> all the values for the qty{n} field,
> but I don't know how to retrieve the value of its Corresponding
> Price{n}.
[snip]
What you're looking for is bracket notation; the ability to construct
property names through string concatenation. This is described in the
FAQ (and elaborated in the notes):
<http://jibbering.com/faq/#propertyAccessAgain>
As you don't know the number of rows, you can leverage the fact that a
nonexistent property will return null to simplify your loop (no need to
use String.prototype.indexOf):
var i = 0,
element;
// 'item' may be a better choice below if that's always
// present in a row and 'qty' isn't, in which case the
// condition below should become a null comparison instead
// of an assignment expression/boolean type-conversion.
while ((element = form.elements['qty' + i])) {
var subtotal = form.elements['qty' + i]
* form.elements['price' + i];
// ...
i++;
}
Alternatively, you could of course inject a script element that contains
a variable initialised with the number of rows when the page is generated.
Hope that helps,
--
Mike Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Back to comp.lang.javascript | Previous | Next — 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