Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8139201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:44:48+00:00 2026-06-06T11:44:48+00:00

Note, this isn’t my code, or the actual code in the form. This is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:44:50+00:00Added an answer on June 6, 2026 at 11:44 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Note: This is not a question about what is the best way with code
Note: this question has nothing to do with Knockout.js, but it's about the selectedOptions
UPDATE: This isn't about getting it to compile. The question is, why does the
Note that this isn't a problem I face, I'm more interested in what is
Note: this isn't one of the millions dups of the common array copy 'problem'
Is there an easier way to do this in Python (2.7)?: Note: This isn't
NOTE: This is a followup to my question here. I have a program that
NOTE: This is an old question and the answers here no longer works (since
Note: this was inspired by WebBrowser Event Properties? Why am I able to access
Note this question was originally posted in 2009, before C++11 was ratified and before

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.