I have the following code, and I need to add the values of the checkboxes instead of concatenating them. Can anyone help me?
<!DOCTYPE html>
<html>
<head>
<style>
#out{width:350px;}
</style>
<script src="/scripts/snippet-javascript-console.min.js?v=1"></script>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputtype="checkbox" name="dias[]" value="0.5">,5
<input type="checkbox" name="dias[]" value="1">1
<input type="checkbox" name="dias[]" value="0.25">,25
<input type="checkbox" name="dias[]" value="0.45">,45
<input type="checkbox" name="dias[]" value="0.5">,5
<input type="checkbox" name="dias[]" value="0.75">,75
<input type="checkbox" name="dias[]" value="0.8">,8
<br>
<input type="text" name="dias" value="" id="out">
<script type="text/javascript">
var inputs = $('input[name="dias[]"]');
inputs.on('change', function () {
var str = [];
var control = 0;
inputs.each(function () {
if (this.checked) {
str.push(this.value);
control++;
}
});
$('input[name="dias"]').val(str.join(', '));
console.log($('input[name="dias"]').val());
});
</script>
</body>
</html>