I have checkbox type inputs and using jquery I am trying to add the numbers which are in its data-price attribute
$(document).ready(function() {
$(".dateboxes").click(function() {
var total = 0;
var price = parseInt($(this).attr('data-price'));
//alert(price);
if ($(this).attr('checked', true)) {
total = total + price;
$(this).prop('checked', this.value == 1);
}
if ($(this).attr('checked', false)) {
total = total + price;
$(this).prop('checked', this.value == 0);
}
alert(total);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-2">
<div class="datebox">
<label for="datebox_8"><span>08th</span> <br/><span>Wed</span></label>
<br/><input data-price="350" type="checkbox" name="dates[]" id="datebox_8" class="dateboxes date-inputs" value="2017-11-08" />
</div>
</div>
<div class="col-md-2">
<div class="datebox">
<label for="datebox_9"><span>09th</span> <br/><span>Thu</span></label>
<br/><input data-price="350" type="checkbox" name="dates[]" id="datebox_9" class="dateboxes date-inputs" value="2017-11-09" />
</div>
</div>
<div class="col-md-2">
<div class="datebox">
<label for="datebox_10"><span>10th</span> <br/><span>Fri</span></label>
<br/><input data-price="350" type="checkbox" name="dates[]" id="datebox_10" class="dateboxes date-inputs" value="2017-11-10" />
</div>
</div>
<div class="col-md-2">
<div class="datebox">
<label for="datebox_11"><span>11th</span> <br/><span>Sat</span></label>
<br/><input data-price="350" type="checkbox" name="dates[]" id="datebox_11" class="dateboxes date-inputs" value="2017-11-11" />
</div>
</div>
But neither it shows right value, nor it marks as checked when clicked.
Please help