Run basic math in real time on a form using JavaScript

Hey @brettlewis, sure! So that would look like this.

Here are the expansion pack instructions.

  1. Add a Decision Box field and add the CSS Classes math-input include-vat

  2. Add the following to the code

     if($('.include-vat input')[0].checked){
         calc = calc * 1.2;
     }
    

So the result in my example looks like this.

TB.render('component_3',function(){
    var calculateAndUpdateDom = function(){
        var amountInput = $('.amount input').val();
        var quantityAmount = $('.quantity input').val();
        var calc = amountInput * quantityAmount;
        if($('.include-vat input')[0].checked){
            calc = calc * 1.2;
        }
        $('#total').html('$'+calc);
    };
    $('.math-input').on('keyup change',function(){
        calculateAndUpdateDom();
    });
});
2 Likes