Yay, time for me to use my favourite picture!
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.
//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:
// 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]; } }
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; };
var checkBoxes = document.querySelectorAll("input[type=checkbox]"), max = checkBoxes.length, i, total = 0, totalWholesale = 0; for (i = 0; i < max; i += 1) { if (checkBoxes[i].checked) { // Get values directly from table cells. total += +checkBoxes[i].parentElement. previousElementSibling.innerHTML; totalWholesale += +checkBoxes[i].parentElement. previousElementSibling.previousElementSibling.innerHTML; } }
total += parseInt(checkBoxes[i].parentElement.previousElementSibling.innerHTML, 10);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
So what you're saying is that I should remove all the 20 individual inspections of the checkboxes, and replace with just this code? And that it will produce the same page?
That doesn't work, and I'm not sure what else I'd need to do.