I am working on a project that involves multiple formulas and cannot figure out how to make the formula flow using jQuery.

The first formula that i am working with is from an excel spreadsheet:

((C5*24-(E5/2))/24)+((D5-(E5/2))/24)

C5 refers to monthly lease or lcost1
E5 Refers to Down Payment or dp1
D5 Refers to Easy Pay or ep1

I have a disabled box that will show the total cost, if there is a down payment or not, and one that will calculate taxes if it is easy pay but not leases.

if dp1 ==0 then just normal elseif dp1==(e.g.100.00) then it does the calc/2
here is my inputs inside a table:

<td><input type="text" name="lcost1" size="10" id="lcost1" /></td> <td><input type="text" name="epay1" size="10" id="epay1" /></td> <td><input type="text" name="dp1" size="10" id="dp1" /></td> <td><input disabled="disabled" size="10" type="text" name="mcost1" id="mcost1" /></td> <td><input disabled="disabled" size="10" type="text" name="devtax1" id="devtax1" /></td>

mcost1 is where the total should be displayed.

This is what i have so far...

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script>
     $(function(){           
            $('input').each(function() {
            $(this).keyup(function(){  
                calculateTotal($(this));
            });
        });
    });

    function calculateTotal(src) {

        var lcost1 = this.input('#lcost1');
        var epay = this.input('#epay1');
        var dp1 = this.input('#dp1');
        var statetax = this.input('#statetax');
        var sumtable = src.closest('#tracker');
    {
        ((lcost1*24-(dp1/2))/24)+((epay1-(dp1/2))/24))
    };
        });
    </script> 
Member Avatar for diafol

Change this in function to src

So i did that, and now i just cannot figure out how to make the actual formula work to display the total in the mCost1 field after the fields are put in.

<script>
$(function(){           
        $('input').each(function() {
            $(src).keyup(function(){  
                calculateTotal($(this));
            });
        });
    });

    function calculateTotal(src) {

        var lcost1 = src.input('#lcost1');
        var epay = src.input('#epay1');
        var dp1 = src.input('#dp1');
        var statetax = src.input('#statetax');
        var sumtable = src.closest('#tracker');
{
        ((lcost1*24-(dp1/2))/24)+((epay1-(dp1/2))/24))
};
        });
</script>

The formula will simplify enormously.

((lcost1*24-(dp1/2))/24)+((epay1-(dp1/2))/24)

can be written :

lcost1 + (epay1 - dp1) / 24

And you probably want to display :

(lcost1 + (epay1 - dp1) / 24).toFixed(2)

ie rounded to 2 decimal places.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.