0

Im creating a form where i want the user to fill out an amount, and then show at the bottom of the form. Then if there is a checkbox checked it adds 21 to the value within the input field so far i have this, but its not quite working.

http://jsfiddle.net/vePbV/

<label>Desired amount</label> <input name="tbDesiredAmount" type="number" id="tbDesiredAmount" min="50" />
<label>Include Apron?</label> <input id="cb_Apron" type="checkbox" name="cb_Apron" /> 

<p>Total: £<span id="total">0</span>.00</p>



$('#tbDesiredAmount').blur(function() {
        var value = $('#tbDesiredAmount').val();
        $("#total").empty().append(value);
});
$('#cb_Apron').blur(function() {
        var value = $('#tbDesiredAmount').val();
        var apron = 21;
        var total = value + apron; 
        $("#total").empty().append(total);
});

So and example of what i want it to do.

  • Type 70 into "desired amount", show 70 in #total when you focus off the input field.
  • Check apron tickbox, adds 21 to the desired amount so displays 91 in #total
  • if you uncheck the apron checkbox, it will remove 21 from the figure in #total
  • if i change the desired amount, it will update the #total, this needs to work with the tickbox checked and the tickbox not checked.

Any help would be greatly appreciated as im rather stuck at the moment.

3
  • 2
    Use parseInt() - var total = parseInt(value) + parseInt(apron); Commented Apr 4, 2014 at 11:14
  • Amount can be decimal too. If so use parseFloat(). Commented Apr 4, 2014 at 11:17
  • Krish R - Cheers, thats sorted the adding the two numbers together. Commented Apr 4, 2014 at 11:21

1 Answer 1

1

Try this Use parseInt()

var apron = 21;
$('#tbDesiredAmount').keyup(function () {
    var value = $('#tbDesiredAmount').val();
    if ($('#cb_Apron').is(':checked')) {
        var total = parseInt(value) + parseInt(apron);
        $("#total").empty().append(total);
    } else {
        $("#total").empty().append(value);
    }
});
$('#cb_Apron').click(function () {
    if ($(this).is(':checked')) {
        var value = $('#tbDesiredAmount').val();
        var total = parseInt(value) + parseInt(apron);
        $("#total").empty().append(total);
    } else {
        var tot = parseInt($("#total").text()) - (parseInt(apron))
        $("#total").empty().append(tot);
    }
});

DEMO

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

2 Comments

If I type 70 in the amount, Total value is 70. When I check the Apron checkbox the Total value is 91. When I uncheck the Apron checkbox the Total value stays 91?
how would i make it so if i changed the desired amount after checking the checkbox it still counts the apron price.

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.