I have a form that has input checkboxes like the ones in the example. The js code extracts the number from the checked input values and outputs them one next to eachother. How to I edit the code so that when you check a second input checkbox the output is the sum of the numbers?
This is the PHP form:
<input type="checkbox" onchange="toggleCheckbox(this)" value="abc 1 def" name="1">
<input type="checkbox" onchange="toggleCheckbox(this)" value="abc 2 def" name="2">
<input type="checkbox" onchange="toggleCheckbox(this)" value="abc 3 def" name="3">
The JavaScript i use:
<script type="text/javascript">
function toggleCheckbox(element){
if (element.checked){
var number = element.value.replace ( /[^\d.]/g, '' );
document.getElementById("test").innerHTML += " " + number;
}
}
</script>
The HTML field:
<p id="test"></p>