2

Note, this isn't my code, or the actual code in the form. This is just my testing version, but does represent what I want to do.

Given the following code, how can I get the script to subtract the passed value when the checkbox is unselected:

<!DOCTYPE html>
<html lang="en">

    <head>
        <title>form test</title>
        <script type="text/javascript" charset="utf-8" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
        <script type="text/javascript">
            function updateAmount(amount) {
                parseInt(amount);
                if ($('#amount').val()) {
                    parseInt(amount);
                    amount = amount + parseInt($('#amount').val());
                }
                $('#amount').val(amount);
                alert('Amount: ' + $('#amount').val());
            }
        </script>
    </head>

    <body>
        <form action="" method="post" name="form-test">
            <input onclick="updateAmount(150)" class="inputbox" id="reservationfee0"
            value="Registration Fee" type="checkbox" />
            <strong>$150</strong>Reservation Fee</form>
    </body>

</html>

As it stands, whoever coded the function didn't take into account that people might check and uncheck that box before they submit the form (which includes more fields and other numbers that get added into the amount variable before submission). There has been a couple of reports of over-charges due to this, so I need to figure out how to stop it from happening, but I know very little about javascript/jquery.

2
  • Do you want to subtract 150 if its unchecked and add 150 when its checked?
    – Wiz
    Jun 25, 2012 at 16:05
  • 1
    You're using parseInt incorrectly in your example. Also, you are referencing an element with an ID of 'amount', yet no such element exists in your example.
    – j08691
    Jun 25, 2012 at 16:07

3 Answers 3

2

Try :

function updateAmount(amount) {
    $amount = $('#amount'); // cache for later use
    if ($('#reservationfee0').is(':checked')) {
        $amount.val(parseInt($amount.val(), 10) + amount);
    } else {
        $amount.val(parseInt($amount.val(), 10) - amount);
    }
}​

Working example here

this checks if the checkbox is checked using .is() if it is then the amount is added to the current value if not its subtracted.

Also note that parseInt takes a second parameter - radix its recommended that you include this value to be certain of results ... docs for parseInt here

1
  • 1
    This, with some extra mods to work with the rest of the actual form, solved the problem perfectly. Thanks!
    – Keith S.
    Jun 25, 2012 at 16:46
0

can you try this

      function updateAmount(amount) {
            parseInt(amount);
            if ($('#reservationfee0').prop('checked')) {
                amount = amount + parseFloat($('#amount').val());
            }
         else{
              amount = parseFloat($('#amount').val())-amount;
         }
            $('#amount').val(amount);
            alert('Amount: ' + $('#amount').val());
        }
0

What we can do is define a variable just above the function which we can set for the first time. The code goes as follows:

       var chk=1;
       function updateAmount(amount) {
            parseInt(amount);
            if ($('#amount').val() && document.getElementById('reservationfee0').checked && chk==1) {
                chk=0;
                parseInt(amount);
                amount = amount + parseInt($('#amount').val());
            }
            $('#amount').val(amount);
            alert('Amount: ' + $('#amount').val());
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.