I got a form with several checkbox's where the user can choose multiple values, like so:
<div>
<input id="myinput1" type="checkbox" value="choice_id_1" />
<label for="myinput1">5</label>
</div>
<div>
<input id="myinput2" type="checkbox" value="choice_id_2" />
<label for="myinput2">10</label>
</div>
<div>
<input id="myinput3" type="checkbox" value="choice_id_3" />
<label for="myinput3">10</label>
</div>
I need to get the values from each :selected checkbox label and return their total sum (ie: total = 25).
So far I have been trying to get the strings from the labels with the .text() method, make an array with the results, converting them to int with parseFloat() and sum the array elements... obviosly with no success.
This is what i got (for sure not the best approach so I'm open to diferent solutions):
function totalSum() {
var prices = $("input:checkbox:checked").siblings("label").text();
var myArray = $.makeArray(prices);
var total = 0;
for(var i = 0; i < myArray.length; i++){
var thisVal = parseFloat(myArray[i]);
if(!isNaN(thisVal)){
total += thisVal;
}
};
$(".mydiv").text(total);
};
totalSum();
$("input:checkbox").change(totalSum);