0

I have a form where based on checkbox total value will be updated

Here is my form

<label class="checkbox">
        <input type="checkbox" class="check" value="23">SEO                             
    </label>
    <label class="checkbox">
        <input type="checkbox" class="check" value="34">XYZ 
    </label>                                
    <label class="checkbox">
            <input type="checkbox" class="check" value="45">Logo Design                                  
    </label>


    <span class="product-price" id="total" >45</span>

jQuery I have used

  $('input[type=checkbox]').change(function () {
        var val = parseFloat(this.value),
            totalVal = parseFloat($('#total').val());
        if (this.checked) {
            $('#total').val((totalVal + val).toFixed(2));
        } else {
            $('#total').val((totalVal - val).toFixed(2));
        }
    });

But nothing get updated.What could be the possible Error? Any solution please

3 Answers 3

1

Span attribute doesn't have a val() property but text()

 

    $('input[type=checkbox]').change(function () {
            var val = parseFloat(this.value),
                totalVal = parseFloat($('#total').text());
            if (this.checked) {
                $('#total').text((totalVal + val).toFixed(2));
            } else {
                $('#total').text((totalVal - val).toFixed(2));
            }
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="checkbox">
        <input type="checkbox" class="check" value="23">SEO                             
    </label>
    <label class="checkbox">
        <input type="checkbox" class="check" value="34">XYZ 
    </label>                                
    <label class="checkbox">
            <input type="checkbox" class="check" value="45">Logo Design                                  
    </label>


    <span class="product-price" id="total" >45</span>

Sign up to request clarification or add additional context in comments.

Comments

0

Change it:

if (this.checked) {
            $('#total').val((totalVal + val).toFixed(2));
        } else {
            $('#total').val((totalVal - val).toFixed(2));
        }

to

if (this.checked) {
            $('#total').text((totalVal + val).toFixed(2));
        } else {
            $('#total').text((totalVal - val).toFixed(2));
        }

as span do not have a value attribute, the text you see is the text attribute associated with that.

Comments

0

Simply do with Array#map function .simply add with checked values only on each time check or uncheck .no need subtraction for below method

$('input[type=checkbox]').change(function() {
var tot=0;
$('input:checked').map(function(a){
 tot += parseInt($(this).val()) 
})
  $('#total').val(tot.toFixed(2));
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<label class="checkbox">
        <input type="checkbox" class="check" value="23">SEO                             
    </label>
<label class="checkbox">
        <input type="checkbox" class="check" value="34">XYZ 
    </label>
<label class="checkbox">
            <input type="checkbox" class="check" value="45">Logo Design                                  
    </label>
    <input id="total">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.