I have this script set up on my page and it all works fine. It basically pulls info from a form and calculates costs. But now, I need a change and I can't get it to do what I need it to do. There are two values that I need to set minimums for; form.height.value and form.linear.value. I have these as defualt values on the form. But, what I want the script to do is this: If the user changes the value to less than a specific number (8 and 50, respectively) the onChange (which calls this function from every input box) will automatically change the value back to the minimum, yet continue to re-calculate. I added an if statement in, only I am not doing something right. It will change the value back, but the rest of the calculations will not continue. I am sure it has to do with logic and how the script "flows". I am new to scripting and flounder my way through it. You can see the basic function at http://www.shayden.com/_WIFPCO/pricing.htm however, I have changed the drop down in height to an entry box. Anyway, as a user enters or changes any of the boxes, the price is re-clculated because the onChange calls the function calculateCost(form). However, when I add the if statement at the beginning to check the minimum and have it re-enter the minimum automatically, only the first one works and does not re-calculate. Can someone offer some help? This is the code I tried adding: var height = form.height.value; if (height <= 50) { var height = 50} form.height.value = height.toFixed(0); var linear = form.linear.value; if (linear <= 50) { var linear = 50} form.linear.value = linear.toFixed(0); -------------------------------------------------------- This is the code that works without the new necessary minimums. function calculateCost(form) { var basecost = form.height.value * form.linear.value * .9375; form.basecost.value = basecost.toFixed(2); var Ornamentcost = form.OrnamentLinear.value * .25; form.OrnamentCost.value = Ornamentcost.toFixed(2); var Rustcost = form.RustLinear.value * .25; form.RustCost.value = Rustcost.toFixed(2); var ExtraOnecost = form.ExtraOneLinear.value * .25; form.ExtraOneCost.value = ExtraOnecost.toFixed(2); var Extra2cost = form.Extra2Linear.value * .50; form.Extra2Cost.value = Extra2cost.toFixed(2); var Colorcost = form.ColorLinear.value * .25; form.ColorCost.value = Colorcost.toFixed(2); var Primercost = form.PrimerLinear.value * .25; form.PrimerCost.value = Primercost.toFixed(2); var Additionalcost = Ornamentcost + Rustcost + ExtraOnecost + Extra2cost + Colorcost + Primercost; form.additionalcost.value = Additionalcost.toFixed(2); var Subtotal = Additionalcost + basecost; form.subtotal.value = Subtotal.toFixed(2); var Tax = Subtotal * .0825; form.tax.value = Tax.toFixed(2); var Totalcost = Subtotal + Tax; form.totalcost.value = Totalcost.toFixed(2); }
What errors do you get when you execute the code? It doesn't seem to have anything wrong in it. Also, where did you add the new code for the minimums check ? Post a bit more info.
I had the new code right at the beginning. It didn't generate errors, but, it wouldn't calculate. Here, take a look at it at http://www.shayden.com/_WIFPCO/pricing3.htm Play with the two inputs. The first one, set to 8 will reset to 8 if you enter a number less than 8. I want the second one to do the same if a number of less than 50 is entered. However, it doesn't if the number in the first box is greater than 8. Secondly, base cost does not calculate past $375.00... the cost of 8 * 50 * .9375. Which means it only calculates when the numbers are at their default or equal to the default values. I know this has something to do with the logic in the IF conditions. The fuction haults once one of the conditions is false. I know it can be fixed with an ELSE statement or set in a different function, but, I can seem to call one function from another. For instance... I could make the first input box call a function to check for minimum, then have it call the calculation function (if I knew how to do that). I could have the second input box call a different function to check for minimum, then also call the calculation function. If I knew how to write the script that would check the minimum, make the necessary changes if needed, and then call the calculation function, regardless of the first condition being true or false, that should solve the issue. But, I just can't seem to make that work either.
Hi jscooter, you try to add this if (height <= 50) { var height = 50} and if (linear <= 50) { var linear = 50} what you do here, is define new variables with the same name as the one declared above that line.. this new variables only have scope inside the blocks you define with the { and } so their value will not persist after that.. just remove the var directive and brackets and it should work fine.. you should also convert the values from the fields to actual numbers to be sure your conditions will work correct.. var height = parseFloat(form.height.value); if (height <= 50) height = 50; form.height.value = height.toFixed(0); var linear = parseFloat(form.linear.value); if (linear <= 50) linear = 50; form.linear.value = linear.toFixed(0); Code (javascript): take care
yup, a local variable that was used globally.. that's another reason why we should use a strict-declared variable.. There are way to many holes when we use an auto generated variables