Get and Sum Dynamic Input Values when MaskMoney.js is applied

649 Views Asked by At

I need some help on sum of the values from arrays on dynamic input add. Please check this code -> https://jsfiddle.net/ricardofranco/gaf32kry/8/ - The maskMoney is applied and I use maskmoney.js as a function. The mask is applied, but it can not sum.

var a = function() {
   $('.value').maskMoney({
            allowNegative: true, 
            thousands:'.', 
            decimal:',', 
            affixesStay: false});    };
$('#add_row').click(function() {

$("#linha").first().clone(true).appendTo("tbody").append("<td data-name='del'><button name='' class='btn btn-danger glyphicon glyphicon-remove row-remove'><span aria-hidden='true' id='row-remove'>-</span></button></td>");   
});

  $("#linha").on('click', '.row-remove' ,function() {
    $(this).closest("#linha").remove();
});

$('.table-primary').on('input', '.value', function(){
var totalSum = 0;
    $('.table-primary .value').each(function(){
        var inputValue = $(this).val();

        if(inputValue){

            totalSum += parseFloat(inputValue);
        }

    });

    $('#total').text(totalSum).innerHTML;

});

If I remove the maskMoney function, it sums ok. Please check this another code -> https://jsfiddle.net/ricardofranco/sgch8qfa/11/ - The maskMoney.js is not applied.

$('#add_row').click(function() {

$("#linha").first().clone(true).appendTo("tbody").append("<td data-name='del'><button name='' class='btn btn-danger glyphicon glyphicon-remove row-remove'><span aria-hidden='true' id='row-remove'>-</span></button></td>");

});

  $("#linha").on('click', '.row-remove' ,function() {
    $(this).closest("#linha").remove();
});

//sum all values
$('.table-primary').on('input', '.value', function(){

    var totalSum = 0;

    $('.table-primary .value').each(function(){
        var inputValue = $(this).val();

        if(inputValue){
            totalSum += parseFloat(inputValue);
        }
    });
    $('#total').text(totalSum).innerHTML;
});

How to get the values and sum when the maskmoney.js is applied? Can you guys help me on this?

1

There are 1 best solutions below

4
Andy Song On BEST ANSWER


var a =  function() {
   $('.value').maskMoney({allowNegative: true, thousands:'.', decimal:',', affixesStay: false});
};

$('#add_row').click(function() {


$("#linha").first().clone(true).appendTo("tbody").append("<td data-name='del'><button name='' class='btn btn-danger glyphicon glyphicon-remove row-remove'><span aria-hidden='true' id='row-remove'>-</span></button></td>");

});

  $("#linha").on('click', '.row-remove' ,function() {
    $(this).closest("#linha").remove();
});


//sum all values
$('.table-primary').on('keyup', '.value', function(){
    var totalSum = 0;
    $('.table-primary .value').each(function(){
        var inputValue = $(this).maskMoney('unmasked')[0];


        if(inputValue){
            totalSum += parseFloat(inputValue);
        }

    });

    $('#total').text(totalSum).innerHTML;

});