0

I have three input field in a page, they appear (.append) in different combinations and there is a fourth div that shows the sum of that combination: it could be: a (is always on), a+b, a+c, a+b+c. I'm using if/else if statement but idoesn't work for a+b+c. Can someone help? thanks. The code i'm using:

    $( document ).ready(function() {
    $(".page").on("change keyup keydown paste propertychange bind mouseover", function(){

    var iva4 = $("#total_amount").val();
    var iva22 = $("#total22_amount").val();
    var iva0 = $("#total0_amount").val();

    if (($("#total_amount").length) && ($("#total22_amount").length)) {

        var totaleIva = (parseFloat(iva4) + parseFloat(iva22));

    } else if (($("#total_amount").length) && ($("#total0_amount").length)) {

        var totaleIva = (parseFloat(iva4) + parseFloat(iva0));

    } else if ((("total_amount").length) && ($("#total0_amount").length) && ($("#total22_amount").length) ) {

        var totaleIva = (parseFloat(iva4) + parseFloat(iva22) + parseFloat(iva0));
    }

    $("#totale-somma").val(parseFloat(totaleIva).toFixed(2));


    if( !$.trim( $('.subtotale').html() ).length ) {
        $('#somma-finale').css("display", "none");
    } else {
        $('#somma-finale').css("display", "block");
    }


   });

   });
4
  • please share html code also and elaborate your problem in more details. Commented Dec 17, 2014 at 8:55
  • are total_amount, total22_amount and total0_amount are getting created dynamically? and is there possibility that any of them may not be present in html DOM? Commented Dec 17, 2014 at 8:58
  • @Bhushan Kawadkar Yes, the divs are added dynamically. Yor solution works perfectly. Commented Dec 17, 2014 at 9:36
  • Great!!, happy to help you. Could you please accept the answer if you find this solution helpful for you. Commented Dec 17, 2014 at 9:39

2 Answers 2

2

The problem is the order of the if blocks

 $(document).ready(function () {
     $(".page").on("change keyup keydown paste propertychange bind mouseover", function () {

         var iva4 = $("#total_amount").val();
         var iva22 = $("#total22_amount").val();
         var iva0 = $("#total0_amount").val();

         if ((("#total_amount").length) && ($("#total0_amount").length) && ($("#total22_amount").length)) {

             var totaleIva = (parseFloat(iva4) + parseFloat(iva22) + parseFloat(iva0));
         } else if (($("#total_amount").length) && ($("#total22_amount").length)) {

             var totaleIva = (parseFloat(iva4) + parseFloat(iva22));

         } else if (($("#total_amount").length) && ($("#total0_amount").length)) {

             var totaleIva = (parseFloat(iva4) + parseFloat(iva0));

         }

         $("#totale-somma").val(parseFloat(totaleIva).toFixed(2));


         if (!$.trim($('.subtotale').html()).length) {
             $('#somma-finale').css("display", "none");
         } else {
             $('#somma-finale').css("display", "block");
         }


     });
 });
Sign up to request clarification or add additional context in comments.

3 Comments

totaleIva variable must be defined outside if/ else otherwise $("#totale-somma").val(parseFloat(totaleIva).toFixed(2)); give you error.
@BhushanKawadkar not required... javascript doesn't have block scope.. except for let
Arun P Johny Thanks for you answer, it works. I accept your answer, but i think i will use [@Bhushan Kawadkar] code because seems a step forward respect to my code. Thanks
1

Assuming that all total fields will be available in html DOM, below is the solution to calculate total -

$( document ).ready(function() {
   $(".page").on("change keyup keydown paste propertychange bind mouseover", function(){
        // below code will convert values to float otherwise to 0
        var iva4 = parseFloat($("#total_amount").val()) || 0;
        var iva22 = parseFloat($("#total22_amount").val()) || 0;
        var iva0 = parseFloat($("#total0_amount").val()) || 0;

        var totaleIva = iva4 + iva22 + iva0;

        $("#totale-somma").val(parseFloat(totaleIva).toFixed(2));


        if( !$.trim($('.subtotale').html()).length ) {
            $('#somma-finale').hide();
        } else {
            $('#somma-finale').show();
        }

    });

});

1 Comment

Thanks Bhushan Kawadkar. Arun P Johny answered my question, but i will use your code, i give you a +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.