How to Perform Calculations from Checkbox inputs

So the HTML code is:

<select id='testSelect1' multiple>
                        <option value='1.2'>Apple</option>
                        <option value='3.3' >Acocados</option>
                        <option value='4.3' >Bannanas</option>
                        <option value='3.32'>Beans</option>

And The JavaScript code so far is as follows:

<script>
	document.multiselect('#testSelect1')
		.setCheckBoxClick("checkboxAll", function(target, args) {
			console.log("Checkbox 'Select All' was clicked and got value ", args.checked);
		})
		.setCheckBoxClick("1", function(target, args) {
			console.log("Checkbox for item with value '1' was clicked and got value ", args.checked);
		});

	function enable() {
		document.multiselect('#testSelect1').setIsEnabled(true);
	}

	function disable() {
		document.multiselect('#testSelect1').setIsEnabled(false);
	}
</script>

When a user checks a box, I want to be able to add up all the values they have selected. How can I do this?

At a spitball, without explanation:

sum = Object.values(document.getElementById('testSelect1').selectedOptions).reduce((x,y) => x + parseFloat(y),0.0);

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.