Search code examples
javascriptjqueryparseint

Jquery - NaN error after running plugin to convert to USD $


$(document).ready(function() {
  $('#clone').click(function() {
    var target = $(this).closest('.groupcontainer');
    target.clone(true, true).insertAfter(target);
  });

  $('.input').on('input',function(){
    $(this).parent().children('.grouptotal').val($(this).val() *  
$(this).parent().children('.input2').val());
  $('.grouptotal').change();
  });

  $('.input2').on('input', function(){
    $(this).parent().children('.grouptotal').val($(this).val() * 
$(this).parent().children('.input').val());
     $('.grouptotal').change();
  });
  $('.input2').dblclick(function(){
    $(this).parent().children('.input2').autoNumeric('init', {aSign: "$ "});
  });  
  $('.grouptotal').dblclick(function(){
    $(this).parent().children('.grouptotal').autoNumeric('init', {aSign: "$ "});
  });
  $('#subtotal').dblclick(function(){
    $(this).parent().children('#subtotal').autoNumeric('init', {aSign: "$ "});
  });
  $('.reset').click(function(){
    $('.behindgroup').parent().children().find('input, textarea').val('');
  });
});  


$(document).on('change', '.grouptotal', function(){
  var sum = 0;
    $('.grouptotal').each(function(){
    sum += +$(this).val();
    });
    $('#subtotal').val(sum);
      });

Here's the Fiddle.

I have a function that does quantity * system = grouptotal.

My issue now is this. I have a plugin (autoNumeric) that will convert .grouptotal to USD$. However, when I double click it to convert, my #subtotal will change from a number to NaN. I've tried putting parseInt in every place I can think of, but it always reverts to NaN after the plugin is run. Prior to running the plugin, the #subtotal is fine, but they are not a dollar amount like I need.

I've changed sum += +$(this).val(); to sum += +parseInt($(this).val()) || 0; but it just ends up returning 0 in #subtotal.

How do I get #subtotal to pull just the number from .grouptotal after I've run my plugin to convert to $USD?

I hope this makes sense..


Solution

  • Using JavaScript, you could take advantage of regular expressions by doing something like this:

     '$45.00'.match(/\d+/)[0]
    

    Being '$45.00' your subtotal.

    This was found here