Forum Moderators: open
Here's what I have now:
function getDiscount() {
var units = document.PriceCalc.Quantity.value;
if (units >= 200) {
var result = 2.99;
}
else if (units >= 100) {
var result = 3.99;
}
else if ((units <= 99) && (units >= 1)) {
var result = 4.99;
}
document.PriceCalc.ItemBasePrice.value = result;
}
Assuming that for any given product there is a variable number of quantity breaks, how would I:
a) store the variables in the HTML of the page? and
b) rewrite the function to accept the variables from the page?
I hypothesize that I could have two hidden text fields (PriceBreaks and BreakPrices) in each page with comma delimited variables and that I would use an array populated with those variables in the javascript function. Am I on the right track or is there a better way?
Thanks for any help you can provide! :)
I hypothesize that I could have two hidden text fields (PriceBreaks and BreakPrices) in each page with comma delimited variables and that I would use an array populated with those variables in the javascript function. Am I on the right track or is there a better way?
var PriceBreaks = new Array(0, 100, 200)
var BreakPrices = new Array(4.99, 3.99, 2.99)
You could do it with one multidimensional array, but using two parallel arrays like this works just as well and is straightforward to understand.
quanLoad = new String(document.PriceCalc.quanArr.value);
var quanArray = quanLoad.split("/");
However, this makes each element of the array be a string and not a number. How do I change each member of a string into a number?
After you've populated the array, make a simple 'for' loop to go through all the items in the array and convert the type of each.
Alternatively, leave the array items as strings, and use parseInt() at the point in the code that you use each value.
Thanks to everyone for their help! Credit will be given in the javascript comments.