quote:
And to be really boring and on topic - attached is an anonymised version of what I was trying to do, although it would then be extended/copy+pasted for multiple countries.
I like how it looks, but I'm sure that the coding is terribly inefficient and when actually extended for lots of countries would probably become unmanageable.
If something can be copied+pasted to deal with multiple things, it's almost always better to write it once to handle multiple things instead, because as you said, it does indeed become unmanageable.
This sort of thing, repeated 20 (!) times:
code:
//Check which checkboxes/options are selected
var f5 = (document.listofoptions.elements['Check5'].checked);
//Check if 'f5' is "true" and set to price, else set to zero
if(f5 == true)
{
var f5 = 50;
var w5 = 30;
}
else
{
var f5 = 0;
var w5 = 0;
}
Could be replaced with a loop like this:
javascript code:
// Checkbox values must be in the format: "normal,wholesale"
var checkBoxes = document.querySelectorAll("input[type=checkbox]"),
max = checkBoxes.length, values,
i, total = 0, totalWholesale = 0;
for (i = 0; i < max; i += 1) {
if (checkBoxes[i].checked) {
values = checkBoxes[i].value.split(",");
// The + on the end is to convert the value to a number.
total += +values[0];
totalWholesale += +values[1];
}
}
If you were using IE < 8, btw, querySelectorAll() would need to be replaced with a custom function to enumerate the checkboxes, which shouldn't be too complicated; probably something like this:
javascript code:
var getCheckboxes = function () {
var i, elements = document.getElementsByTagName("input"),
max = elements.length,
result = [];
for (i = 0; i < max; i += 1) {
if (elements[i].type === "checkbox") {
result.push(elements[i]);
}
}
return result;
};
EDITED: 11 Feb 2011 17:33 by CAER