Calculating dynamically named fields

1. AB
Junior Member
Join Date
Dec 1969
Posts
17

Calculating dynamically named fields

I have a form with field names that are generated on the fly, e.g. 0ProdCost, 1 ProdCost, 2ProdCost etc. They are validated on entry to ensure they do not exceed upper or lower limits. I have to add to this a calculate function to show the total cost at the end of the form, which I have done but now I have noticed a huge mistake. If a user goes back to an earlier figure and changes it, the total is further accumulated regardless of whether the amount goes up or down.<BR><BR>I thought I could rectify this with a for loop that loops once per the number of rows in the form (sounds easy enough!) However, it seems the for loop does not like calculating the field name in the same way that it does outside of the loop. The field name constantly comes up as undefined and the total is obviously a var type error as it is NaN. If anyone can find a better way to write the code below, it would be greatly appreciated.<BR><BR>function calculateCost(fld,cnt){<BR> form = fld.form;<BR><BR> var oldOrderCost = form.elements["tmpOrderCost"].value;<BR> var count = form.elements["tmpCount"].value;<BR> var copies = form.elements[cnt + "txtCopies"].value;<BR> var tempcopyCost = form.elements[cnt + "txtPrice"].value;<BR> var tempprodCost = copies * tempcopyCost;<BR> var totalCost = 0;<BR> <BR> for (i = 0; i &#060;= count; i++)<BR> {<BR> var currentProduct = eval(&#039;form.elements[ i + "prodCost"]&#039;.value);<BR> alert ("Product name is: " + currentProduct);<BR> if (currentProduct &#062; 0);<BR> totalCost = totalCost + currentProduct;<BR> }<BR> alert ("Total cost is: " + totalCost);<BR> <BR> tempCalc = parseInt(oldOrderCost) + parseInt(tempprodCost);<BR> var nextFld = cnt + 1;<BR> prodCost = formatCurrency(tempprodCost);<BR> orderCost = formatCurrency(tempCalc);<BR> form.elements[cnt + "txtProductCost"].value = prodCost;<BR> form.elements[cnt + "Cost"].value = prodCost;<BR> form.elements["tempCalc"].value = tempCalc;<BR> form.elements["txtOrderCost"].value = orderCost;<BR> form.elements["oCost"].value = orderCost;<BR><BR>By the way, I know there is some excess code here, but I haven&#039;t finished cleaning up after trying a few different things.<BR><BR>Regards<BR><BR>AB<BR>

2. Senior Member
Join Date
Dec 1969
Posts
758

Rule # 1

No matter what anybody says... don&#039;t ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever, ever begin your variable names with numbers... <BR>that might actually help you with your "NaN" errors. try it and post back here when you&#039;re done.<BR><BR>Jason

3. AB
Junior Member
Join Date
Dec 1969
Posts
17

RE: Rule # 1

Thanks for the response Jason, but I don&#039;t understand the reasoning behind not using numbers to name fields? I have used them all the way throught the site with no probs, and people like Akhilesh, Bill W and desert Ghost have given me a hand at different times and have not made comment about the practice. I&#039;m not sure of your background in programming, but i&#039;ve no doubt you have probably done more than myself so your answer to this question is appreciated.<BR><BR>Regards<BR><BR>AB

4. django reinhardt Guest

RE: Rule # 1

i didn&#039;t even know you COULD start a variable name with a number

5. Senior Member
Join Date
Dec 1969
Posts
11,334

You can't

Naming Variables<BR>JScript is a case-sensitive language, so naming a variable myCounter is different from naming it MYCounter. In addition, variable names, which can be of any length, must follow certain rules: <BR>The first character must be a letter (either uppercase or lowercase) or an underscore (_), or a dollar sign (\$). <BR>Subsequent characters can be letters, numbers, underscores, or dollar signs. <BR>The variable name can&#039;t be a reserved word. <BR><BR><BR>Some invalid variable names: <BR>99Balloons // Starts with a number. <BR>Smith&Wesson // Ampersand (&) is not a valid character for variable names

6. django reinhardt Guest

that's what i thought <eop>

.

7. Senior Member
Join Date
Dec 1969
Posts
11,334

RE: Calculating dynamically named fields

Try building the variables differently...<BR><BR>Name first, then number:<BR><BR>Product0<BR>Product1<BR><BR>etc<BR> <BR>You can&#039;t start a variable&#039;s name with a number.<BR><BR>Alternatively (and I don&#039;t know your entire set up) you can check out all the form&#039;s elements -- and only decide to see if it&#039;s &#062; 0 based on the type of field. Would be silly to do it that way if you had a considerable amount of form elements.<BR><BR>Or you could just load them into an array, and iterate through that. May take some typing at first though.

8. Senior Member
Join Date
Dec 1969
Posts
96,118

Misplaced quotes...

&nbsp;<BR>You have a major problem in this line:<BR>&nbsp; &nbsp; var currentProduct = eval(&#039;form.elements[ i + "prodCost"]&#039;.value); <BR><BR>Let&#039;s break down that eval:<BR>&nbsp; &nbsp; eval(&#039;form.elements[ i + "prodCost"]&#039;.value); <BR><BR>Remember, eval wants a *SINGLE STRING* as its argument. You have a string, okay, but then you put the .value outside the string. What does that do? Answer: The .value of a string is...UNDEFINED!<BR><BR>So you are asking eval to evaluate something that is undefined! And, thanks to the rules of JS, the result of that is...you guessed it...undefined!<BR><BR>So now these lines:<BR><BR>&nbsp; &nbsp; var currentProduct = eval(&#039;form.elements[ i + "prodCost"]&#039;.value); <BR>&nbsp; &nbsp; alert ("Product name is: " + currentProduct); <BR><BR>*SHOULD* produce an alert that says something like "Product name is: undefined". <BR><BR>And then this line:<BR><BR>&nbsp; &nbsp; if (currentProduct &#062; 0); <BR><BR>*should* blow up, as you are asking to compare an undefined value to an integer. NaN, indeed.<BR><BR>*********************<BR><BR>Why not just get rid of the eval? It&#039;s 100% unneeded. You don&#039;t use it anyplace else in similar code.<BR><BR>for (i = 0; i &#060;= count; i++)<BR>{<BR>&nbsp; &nbsp; // you really WANT a number, so why not ensure you have one!<BR>&nbsp; &nbsp; var currentProdCost = parseFloat( form.elements[ i + "prodCost"].value );<BR>&nbsp; &nbsp; // debug:<BR>&nbsp; &nbsp; alert ("Product cost is: " + currentProdCost);<BR>&nbsp; &nbsp; // why test for &#062; 0??? What harm if you add zero?<BR>&nbsp; &nbsp; // instead, check for a number!<BR>&nbsp; &nbsp; if ( ! isNaN( currentProdCost ) ) totalCost = totalCost + currentProdCost;<BR>}<BR>// debug:<BR>alert ("Total cost is: " + totalCost);<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
96,118

NOT a variable name question!!!

This is a FORM FIELD NAME we are discussing. And form field names can be ANY STRING AT ALL. <BR><BR>&#060;FORM Name="theForm" ...&#062;<BR>&#060;INPUT Name="123 This & that ! &#039;even embedded single quotes&#039; !@#\$" Value=17&#062;<BR>...<BR>&#060;/FORM&#062;<BR><BR>&#060;SCRIPT&#062;<BR>var fld = document.theForm.elements["123 This & that ! &#039;even embedded single quotes&#039; !@#\$"];<BR>var fldValue = fld.value;<BR>...<BR>&#060;/SCRIPT&#062;<BR><BR>NOW...if you *do* use form field names like this, then you can&#039;t use the simpler way of referring to them:<BR><BR>var fld = document.theForm.123 This & that ! &#039;even embedded single quotes&#039; !@#\$;<BR><BR>Not surprisingly, that doesn&#039;t work. But so long as you use the associative array method of accessing them (that is, passing the name as the "key" value to the form.elements collection), you can name them [nearly] anything you want!<BR><BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
96,118

Form field names NOT variable names

See my other posting.<BR><BR>Variable names have to follow the rules.<BR><BR>Form field names (and even form names) do not. But if they don&#039;t follow the rules then you can&#039;t use them in "simple" JS names.<BR><BR>You pays your money, you takes your choice. Me, I always make my form field names *also* be legal variable names, but you sure don&#039;t have to.<BR><BR>

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•