0

I have a set of input fields that automatically calculate the sum and product based on the users input. I want to work on the part wherein it will automatically add a comma too and still got the right sum while typing. Here is what I have so far.

<table>
    <tr>
    <td><input class="cell3" type="text" name="com1" id="com1" value=""></td>
    <td><input class="cell3" type="text" name="com2" id="com2" value=""></td>
    <td><input class="cell3" type="text" name="com3" id="com3" value=""></td>
    <td><input class="cell3" type="text" name="com4" id="com4" value=""></td>
    <td><input class="cell3" type="text" name="com5" id="com5" value=""></td>
    </tr>
 </table>

JScript

$(document).ready(function () {
        $(this).keyup(function aa() {

            $("input[name='com2']").each(function (index) {
                var com1 = $("input[name='com1']").eq(index).val();
                //var com2 = $("input[name='com2']").eq(index).val();
                var com2 = parseFloat(com1) * 0.15;

                if (com1 == "") {
                    com2 = 0;
                }

                if (com2 == "") {
                    com1 = 0;
                }

                else {
                    var com1 = parseFloat(com2) * 0.15;
                }

                if (!isNaN(com2)) {
                    $("input[name='com2']").eq(index).val(com2.toFixed(2));
                }

            });


            $("input[name='com3']").each(function (index) {
                var com1 = $("input[name='com1']").eq(index).val();
                var com2 = $("input[name='com2']").eq(index).val();
                var com3 = parseFloat(com1) - parseFloat(com2);

                if (com1 == "") {
                    com2 = 0;
                }

                if (com2 == "") {
                    com1 = 0;
                }

                else {
                    var com1 = parseFloat(com2) * 0.15;
                }

                if (!isNaN(com3)) {
                    $("input[name='com3']").eq(index).val(com3.toFixed(2));
                }

            });

            $("input[name='com4']").each(function (index) {
                var com3 = $("input[name='com3']").eq(index).val();
                //var com2 = $("input[name='com2']").eq(index).val();
                var com4 = parseFloat(com3) * 0.12;

                if (com3 == "") {
                    com4 = 0;
                }

                if (com4 == "") {
                    com3 = 0;
                }

                else {
                    var com4 = parseFloat(com3) * 0.12;
                }

                if (!isNaN(com4)) {
                    $("input[name='com4']").eq(index).val(com4.toFixed(2));
                }

            });


            $("input[name='com5']").each(function (index) {
                var com3 = $("input[name='com3']").eq(index).val();
                var com4 = $("input[name='com4']").eq(index).val();
                var com5 = parseFloat(com3) + parseFloat(com4);

                if (com1 == "") {
                    com5 = 0;
                }


                else {
                    var com5 = parseFloat(com3) + parseFloat(com4);
                }

                if (!isNaN(com5)) {
                    $("input[name='com5']").eq(index).val(com5.toFixed(2));
                }

            });


        });

        window.onload = aa();
    });

and here's a fiddle for a demo : https://jsfiddle.net/abopmagd/ any help would be much appreciated. Thank you!

x'tian
  • 734
  • 2
  • 14
  • 40
  • I'm looking at your demo and am sort of confused. Guessing the far left field is user input. So the next two fields are what you need to add together to make the user input? and the far right two are if you multiply them together? It doesn't seem to work for me - or I'm assuming wrong. – Jamin Apr 24 '17 at 05:26
  • So there are two parts to this: 1. [Adding the commas](http://stackoverflow.com/questions/3883342/add-commas-to-a-number-in-jquery), and 2. [Removing them](http://stackoverflow.com/a/13303576/157247) before parsing for the calculation. What are you stuck on? – T.J. Crowder Apr 24 '17 at 05:30
  • @Jamin the first input field is a user supply field and the second one is the 15% of the first field and the third one is input1 - input2. While input4 is 12% of input3 while the last input is input3 + input4. Sorry to make you confused. Forgot to add label for easy idetification. – x'tian Apr 24 '17 at 05:40
  • @T.J.Crowder I'm stuck with both. Cause the last time I tried adding commas and replacing it the calculations is not working. Any suggestion or idea to do it right? – x'tian Apr 24 '17 at 05:42
  • @x'tian: Yes. I linked them above. – T.J. Crowder Apr 24 '17 at 05:44
  • @T.J.Crowder Hi I updated my fiddle can you please check which part should I need to add replace method. https://jsfiddle.net/abopmagd/10/ – x'tian Apr 24 '17 at 05:52

0 Answers0