$(document).ready(function(){
$("input[type='checkbox']").click(function(){
var priceTotal = 0;
$("#calculator input[type='checkbox']:checked").each(function() {
priceTotal += parseInt(this.value, 10);
});
$('#total').html(priceTotal);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="calculator">
<input type="checkbox" />$29<br/>
<input type="checkbox" />$39<br/>
<input type="checkbox" />$49<br/>
<input type="checkbox" />$59<br/>
</div>
<div class="total"><span>$0</span></div>
Task: Based on the checkboxes Prices should be added in total class.
I tried this:
$(document).ready(function(){
$("input[type='checkbox']").click(function(){
var priceTotal = 0;
$("#calculator input[type='checkbox']:checked").each(function() {
priceTotal += parseInt(this.value, 10);
});
$('#total').html(priceTotal);
});
});
I want when picking up the numerical values from checkboxes the currency sign should be stripped and later added.
Reason: It will be used finally in Sambhal, setting where the currency may not be a dollar in all the cases.
The best policy will be to get the currency in some data-set and then transfer it to the JQuery.
Please help me to accomplish this.
I do not want to use value inside input.
can we use this kind of HTML:
<div class="inputbox">
<input type="checkbox" id="option1">
<label for="option1">Services</label><span class="license_price bold">€29</span>
</div>
When checkbox ticked we can get the price of that span through class? That’s the demand actually.
4
Answers
There are severel errors:
You didn’t have set any values to your inputs. So the value they return is just
on
oroff
.Set them directly as attribute.
Then you select the output wrong. Your html total has a class, you select a id. Plus you set the html wrong.
Take a look at this fiddle: https://jsfiddle.net/o260skn9/
You have 2 problem:
1 there is no value property on your input.
2 you are using
$('#total')
and not$('.total')
Demo
Considering the html the value corresponding to the checkbox could be retrieved with
.nextSibling.textContent
and you need to change the total selector to.total
as you have it as a class and not id: