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 9280575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:54:14+00:00 2026-06-18T17:54:14+00:00

My basic setup is that I have a form, which is hidden, until a

  • 0

My basic setup is that I have a form, which is hidden, until a selection is made via click, such as “member, nonmember, staff etc.” Once the selection is clicked the form shows, with the the billing area inside also hidden, until the form total reaches greater than $0.

Now the problem I have is that, as soon as 4 of the 5 choices are clicked, the total should instantly be greater than $0. And that being so, the billing portion should trigger automatically, but I can’t get my second function (the one that does the calculations) to listen to the first function for the onclick event or on change event. Is there a way that inside each of my “if” statements, and the end I put in code like “execute calculate function“?

Something like this:

if(section == 'Member') {
            var base_cost = 150;

            $("#commentTable,#ClinicStaff,#IMSNonMember,#Resident,#Student").fadeOut('slow', function() {
                // Animation complete.
            });
            $("#commentTable, #IMSMember").fadeIn('slow', function() {
                // Animation complete
            });
            $("input[name=SectionChosen]").val('Member');// Section selected, pass as hidden field for processing
                    ***$runNextFunction();***

        }

I tried doing: $(" 'input[name^=PROD]', 'base_cost ").change(function() {

to see if I can make the function watch for changes on both the input and the base cost field. But it doesn’t want to trigger. Here is my full setup:

jQuery(function(ready){

    jQuery('.showForm').click(function(){

        var section = $(this).attr('target');
        var base_cost = ''; // reset base cost
        $("input[name=TOTAL]").val('');// Reset the Total field to 0 to hide the billing form again
        $("input[name^=PROD]").val('');// matches those that begin with 'PROD' and clears them
        $("input[name=SectionChosen]").val('');// Reset the section choice


        if(section == 'Member') {
            var base_cost = 150;

            $("#commentTable,#ClinicStaff,#IMSNonMember,#Resident,#Student").fadeOut('slow', function() {
                // Animation complete.
            });
            $("#commentTable, #IMSMember").fadeIn('slow', function() {
                // Animation complete
            });
            $("input[name=SectionChosen]").val('Member');// Section selected, pass as hidden field for processing

        }
        else if(section == 'Clinic') {
            var base_cost = 150;

            $("#commentTable,#IMSMember,#IMSNonMember,#Resident,#Student").fadeOut('slow', function() {
                // Animation complete.
            });
            $("#commentTable, #ClinicStaff").fadeIn('slow', function() {
                // Animation complete
            });
            $("input[name=SectionChosen]").val('Clinic');// Section selected, pass as hidden field for processing
        }
        else if(section == 'Nonmember') {
            var base_cost = 250;

            $("#commentTable,#IMSMember,#ClinicStaff,#Resident,#Student").fadeOut('slow', function() {
                // Animation complete.
            });
            $("#commentTable, #IMSNonMember").fadeIn('slow', function() {
                // Animation complete
            });
            $("input[name=SectionChosen]").val('Nonmember');// Section selected, pass as hidden field for processing
        }
        else if(section == 'Resident') {
            var base_cost = 75;

            $("#commentTable, #IMSMember,#ClinicStaff,#IMSNonMember,#Student").fadeOut('slow', function() {
                // Animation complete.
            });
            $("#commentTable, #Resident").fadeIn('slow', function() {
                // Animation complete
            });
            $("input[name=SectionChosen]").val('Resident');// Section selected, pass as hidden field for processing
        }
        else if(section == 'Student') {
            var base_cost = 0;

            $("#commentTable,#IMSMember,#ClinicStaff,#IMSNonMember,#Resident").fadeOut('slow', function() {
                // Animation complete.
            });
            $("#commentTable, #Student").fadeIn('slow', function() {
                // Animation complete
            });
            $("input[name=SectionChosen]").val('Student');// Section selected, pass as hidden field for processing
        }


        // Calculate order Total
        $('input[name^=PROD]').change(function() { 

            // Total cost of order, is equal to base cost + any additional selectoins made below
            var order_total = base_cost;

            // Run through all the form fields
            $('input[name^=PROD]').each(function(i) {

                // Get the current field and its name
                var form_name = $(this).attr('name');

                // If so, extract the price from the name
                var item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1));

                // Get the quantity
                var item_quantity = parseInt($(this).val(), 10);

                // Update the order total
                if (item_quantity >= 0) {
                    order_total += item_quantity * item_price
                }

            });

            // Display the total rounded to two decimal places
            $('input[name=TOTAL]').val((Math.round(order_total*100)/100).toFixed(2));

            // Show the billing portion if total is not 0
            if (order_total > 1) {
                // show the section with the billing portion
                $("#BillingPortion").show();
            }
            else {
                // hide billing portion if total is 0
                $("#BillingPortion").hide();
            }
        });


    });
});

As you can see from the jsFiddle here the form works, if you click a selection, then add more “cost” by inserting a number into the guests, and THEN the billing portion shows. But that’s not good enough, because if they don’t want any guests, the billing portion should still show if they base cost is greater than $0.

If someone could point me in the right direction that would be great.

Also I’m quite inexperienced when it comes to jQuery, so I’m sure my code could be made more DRY and probably looks awful to some of you, but at least it’s a start.

  • 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-18T17:54:16+00:00Added an answer on June 18, 2026 at 5:54 pm

    All right, I went ahead and did a few refactorings on your code. Here’s the result.

    Markup of section links

    You were using links to switch between forms. However, the links themselves indirectly (through your JavaScript code) set a hidden input called SectionChosen. You could make it more apparent that clicking a link represents a choice, and that choice is also part of the form data. Therefore, it is much more appropriate to use a set of radio boxes to pick one option, rather than set of semantically unrelated links. Since radio boxes are proper input elements with a name and a value, these can replace your original hidden SectionChosen input. Bonus: you don’t even need any JavaScript to set the value, as the value of the selected radio box will be submitted!

    <label><input type="radio" name="SectionChosen" value="Member" /> I am an IMS Member Physician</label>
    <label><input type="radio" name="SectionChosen" value="Clinic" /> I am a Clinic/Hospital Administrators or Clinic Staff</label>
    ...
    

    Retrieving the base cost

    In the click handler, you were trying setting a local variable called base_cost depending on the clicked link to calculate the total. The problem is that you were binding new handlers to the input‘s change event, stacking upon previously bound handlers from previously chosen links. It looks like it’s working, but really you’re calculating a multitude of totals with only the last bound handler producing the final output value.

    In reality, the base cost is a property of the chosen section and it should be readily accessible both when switching sections as when changing form inputs. A good way to do this is by storing the base cost of each choice as an attribute on the radio box. data-* attributes were introduced specifically for such custom data needs:

    <input type="radio" name="SectionChosen" value="Member" data-base-cost="150" />
    

    The base cost can then be retrieved with:

    var base_cost = $(...).data('base-cost');
    

    To get the base cost from the currently selected section, simply look for the currently selected radio box:

    var base_cost = $('input[name=SectionChosen]:checked').data('base-cost');
    

    That’s all there is to it! You no longer need to bind the change handler from within the click handler, as you can find the base cost in the markup itself. This means you only need to bind the change handler once at DOM ready, as it should be.

    Further improvements

    There’s always room for more improvement. For example, you could give the actual form sections a data-section attribute matching the section name instead of an id. This way, you can more easily select the sections which need to be hidden and those that need to be shown. Instead of writing down the same list of ID selectors, you can just select something like .section[data-section="Member"] and .section:not([data-section="Member"]). Heck, that data-section value can even come from the chosen radio box itself, you won’t even need a long if...else if tree any more!

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

Sidebar

Related Questions

I have a parent-child relationship setup that is fairly basic. The end result is
I have a basic form with controls that are databound to an object implementing
The basic setup is classic - you're creating a Windows Forms application that connects
I have a basic invoice setup with models: Invoice, Item, LineItems. # invoice.rb class
I have setup the basic LAMP server on Ubuntu 11.10 and had a few
I have to setup a basic JAVA application including a GUI based on swing.
So I wrote a basic class which I have extended to create html element.
I am using Drupal 7.15, and have a Webform setup as a block that
I have a very basic hide/show div function set up for when people click
I have a basic form-login authentication in my app, and I set up a

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.