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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:22:39+00:00 2026-05-17T18:22:39+00:00

Greetings, I am trying to code a solution for an order form pricing calculator

  • 0

Greetings,

I am trying to code a solution for an order form pricing calculator using the excellent calculation plugin. My form has two different prices for each item, one for quantities under 10 and one for quantities over that amount.

This is the code I have so far:

$(document).ready(function() {

    // bind the recalc function to the quantity fields
    $("input[name^=qty_item_]").bind("keyup", recalc);
    // run the calculation function now
    recalc();

    function recalc() {

        var quantity = $("input[name^=qty_item_]").val();
        var quantity_int = parseInt(quantity_str);
        var threshold = 10;
        if (quantity_int < threshold) {
            var useprice = $("[id^=price_item_1_]");
        } else {
            var useprice = $("[id^=price_item_2_]");
        }

        $("[id^=total_item_]").calc(

            // the equation to use for the calculation
            "qty * price",
            // define the variables used in the equation, these can be a jQuery object
            {
                qty: $("input[name^=qty_item_]"),
                price: useprice
            },
            // define the formatting callback, the results of the calculation are passed to this function
            function (s){
                // return the number as a dollar amount
                return "$" + s.toFixed(2);
            },
            // define the finish callback, this runs after the calculation has been complete
            function ($this){
                // sum the total of the $("[id^=total_item]") selector
                var sum = $this.sum();
                $("#grand_total").text(
                    // round the results to 2 digits
                    "$" + sum.toFixed(2)
                );
            }

        );

    }

});

It’s close, but useprice stays at the value it is set for for the first item in the field array. I’m thinking I need to bring the useprice calculation ‘inside the loop’ somehow, but I’m stuck as to how.

UPDATE: Demo page here.

As per usual, any & all help gratefully received. TIA 🙂

  • 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-05-17T18:22:40+00:00Added an answer on May 17, 2026 at 6:22 pm

    I’ve made a few changes to your code. Seems to work now. The main change was to recalculate only the row that has been changed, instead of recalculating every row every time. The total is still calculated from all totals, of course. I’ve also used event delegation to minimize the bound event handlers, which would have an impact on performance and resource usage for large tables.

    jQuery(
        function($)
        {
            // threshold that defines at what quantity to use the discounted price
            var discountThreshold = 10;
    
            // bind the recalc function to the quantity fields
            // use event delegation to improve performance
            $("#frmOrder")
                .delegate(
                    "input[name^=qty_item_]",
                    "keyup",
                    recalc
                );
    
            // run the calculation function once on every quantity input
            $("input[name^=qty_item_]").trigger("keyup");
    
            // recalculate only the changed row (and the grand total)
            function recalc(e) {
                // input field that triggered recalc()
                var
                    $this = $(this),
                    $parentRow = $this.closest('tr'),
                    quantity = $this.parseNumber()[0],
                    $usePrice = ((discountThreshold <= quantity) ? $("[id^=price_item_2_]", $parentRow) : $("[id^=price_item_1_]", $parentRow)) || 0;
    
                // recalculate the row price
                $("[id^=total_item_]", $parentRow).calc(
                    // the equation to use for the calculation
                    "qty * price",
                    // define the variables used in the equation, these can be a jQuery object
                    {
                        qty: $this,
                        price: $usePrice
                    },
                    // define the formatting callback, the results of the calculation are passed to this function
                    function (s){
                        // return the number as a dollar amount
                        return "$" + s.toFixed(2);
                    },
                    // define the finish callback, this runs after the calculation has been complete
                    function ($that){
                        // sum the total of the $("[id^=total_item]") selector
                        var sum = $("[id^=total_item_]").sum();
                        $("#grand_total").text(
                            // round the results to 2 digits
                            "$" + sum.toFixed(2)
                        );
                    }
                );
            }
        }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings all, I am trying to download 'gz' file using URL class .Code snippet
Greetings all, I'm trying to localize a .NET/C# project. I'm using string resource files
Greetings, coders, Background Info and Code I am trying to create a daemon-type program
Greetings, I'm trying to find a way to 'unbind' a socket from a particular
Greetings, I'm trying to find either a free .NET library or a command-line executable
Greetings! I am trying to check directory write-permissions from within a Windows MFC/ATL program
Greetings stackers, I'm trying to come up with the best database schema for an
Greetings, The VBA code below will create an Excel QueryTable object and display it
Greetings, I am trying to perform a copy from one vector (vec1) to another
greetings, im trying to implement an image rollover on a collection of PictureBox (es)

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.