Run Basic Math in Real Time on a Form

Please note that this is now available as a plugin for easier installation. While you can use this document as a reference, the recommended method to add…

  • Not yet re-checked
  • Classic builder

Moved from our previous help site and not yet re-checked against the current product. Last edited Sep 20, 2024.

Basic formulas work great, but sometimes, you’ll want to display a simple calculation on the form in real time.

Here’s an example of how this could look using our custom code features. In the GIF below, we multiply Amount by Quantity and display the result above the save button.

First, we’ll need to add the following to the HTML source code <> inside a Form Component.

Text
<p id="total">$0.00</p>
Screenshot for “Run Basic Math in Real Time on a Form”

Next, you’ll need to add a class to each field you would like to include in the calculation.

Here, I’ve added a class called “math-input” to both the Amount and Quantity and then unique classes of “amount” and “quantity” for each field. The Amount field would read “math-input amount” and the Quantity field “math-input quantity,” as shown in the image below.

Screenshot for “Run Basic Math in Real Time on a Form”

Next, we need to find the Component ID. This can be found by hovering over the info icon in the Page Builder.

Screenshot for “Run Basic Math in Real Time on a Form”

Finally, we’ll add the JavaScript Code to the JavaScript tab of the page.

Text
TB.render('component_3', function() {
    var calculateAndUpdateDom = function() {
        var amountInput = $('.amount input').val();
        var quantityAmount = $('.quantity input').val();
        var calc = amountInput * quantityAmount;
        $('#total').html('$' + calc);
    };
    $('.math-input').keyup(function() {
        calculateAndUpdateDom();
    });
});
Screenshot for “Run Basic Math in Real Time on a Form”

Was this page helpful?

Something wrong or missing on this page? Tell us.