//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.
It's probably just me being anally retentive, but I bloody hate innerHTML, it's not part of the DOM and it's queer.
And by "queer" I mean gay and homosexual, not as in "by jove, that's a rum do and no mistake".
And by "gay" I mean queer and homosexual, not "having a gay old time, dahling".
It's a wonderful thing, this English language malarkey, isn't it?
Object doesn't support this property or method (line 104, char 1)
Line 104 is:
var checkBoxes = document.querySelectorAll("input[type=checkbox]"),
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; };Then change the line with the error so it reads:
var checkBoxes = getCheckboxes,If that still doesn't work, post the code you have and I'll see if I can spot the problem.
Here you go - it now complains that f5 isn't defined (which sort of makes sense as I used to use that to calculate my totals).
I could remove the totaliser line (line 135) but no idea what to put in it's place.
As for why I'm reading a value from a table, have a look at the file in IE and it should make more sense. This is about choosing a base product from a choice of 4, and then adding various options.
Radio boxes are used for the initial selection, and then checkboxes for the rest. The total then displays the price for your selection.