Search code examples
jquerysumclickkeyup

jQuery keyup or click not responding right away, why?


I have a row of inputs for quantities and I sum the total. I have it working, but there seems to be a lag for when the price total shows up!

The quantity summing works nicely. The price summing seems to happen after clicking, or tabbing, or using the number input select arrows, although not every time! I am using the "keyup" and "click" in the .on function.

Anyway to get this to work the way the quantity summing is working?

JSFIDDLE https://jsfiddle.net/Bigfootbud1/ht573vps/7/

**THIS PRICE SUMMING CODE BELOW DOES NOT RESPOND RIGHT AWAY!**
// ********** LADIES PRICE TOTALS **********
$(document).on('click keyup', '.item1l', function() {
$('.item1l').each(function() {
var qty = parseInt($('.item1ltotal').val(), 10);
var price = parseFloat($('.price').text());
$('#total').text((qty * price ? qty * price : 0).toFixed(2));
});
});

**THIS CODE BELOW WORKS GREAT FOR SUMMING QUANTITY TOTALS - NICELY 
RESPONSIVE - NO ISSUES WITH IT**
// ********** LADIES QUANTITY TOTALS **********
$(document).on('click keyup', ".item1l", function() {
var sum = 0;
$(".item1l").each(function() {
    sum += +$(this).val();
});
$(".item1ltotal").val(sum);    
});

Solution

  • That's because you are separating stuff that respond to the same event(s). Put them together

        $(document).on('click keyup', '.item1l', function() {
          $('.item1l').each(function() {
            // **THIS PRICE SUMMING CODE BELOW DOES NOT RESPOND RIGHT AWAY!**
            // ********** LADIES PRICE TOTALS **********
            var qty = parseInt($('.item1ltotal').val(), 10);
            var price = parseFloat($('.price').text());
            $('#total').text((qty * price ? qty * price : 0).toFixed(2));
    
            //   SUMMATION OF CATEGORY QTY TOTALS
            // ********** LADIES QUANTITY TOTALS **********
            var sum = 0;
            $(".item1l").each(function() {
              sum += +$(this).val();
            });
            $(".item1ltotal").val(sum);
          });
        });
    

    https://jsfiddle.net/luenib/k2nj6d8w/