You're pretty much there... But what should happen if one or more of these are empty?
There should be no issue. Most users will only want 1-2 of these items. They are not required to input values into any of the fields for items they dont need.
I don't think you're following me... Unless you specifically tell the script not to execute if one of the fields is empty, it will treat it as zero, and the results might not be what you expect them to be.
It would be fine if it counts any blanks as zero.
So using the 4 inputs, if someone entered:
1 x 190 gal
x 200 gal
3 x 190 pt
2 x 200pt
I would want the form to show them their cost as ((1+0)*8.75)+((3+2)*35/24) = $16.04, and to have the total charge update each time they changed a number (not requiring them to click on a "calcuate total" button if at all possible)
Does that answer your question?
Use this calculation code, then:
var s1 = +this.getField("190 gal").valueAsString;
var s2 = +this.getField("200 gal").valueAsString;
var s3 = +this.getField("190 pt").valueAsString;
var s4 = +this.getField("200 pt").valueAsString;
event.value = ((s1+s2)*8.75)+((s3+s4)*35/24);
Set the Format of the field to Number with the currency symbol selected to have it automatically added.
wow, thought there would be more to it! That worked perfectly. your help was greatly appreciated!
You should not use valueAsString when adding field values unless you explicitly convert the strings to numbers (which is what try67 did with the "+" operator), though in this specific case it doesn't look like it will make a difference. But it's good not to get in the habit.
I only use the 'valueAsString" property to test if a numeric field is a null string. Note that a null string is different than a zero value field.
You could also use the "Number" constrictor to convert number character strings to floating point numbers.