0

I'm using the autoNumeric jQuery plugin to place a dollar sign and a decimal into a a couple of text fields. Now I want to add these two values, but I'm getting the NAN error because of the dollar sign that is placed in front of the number.

How do I remove the dollar sign (first character), add the values and place the total value into another field?

Sample HTML

<input type="text" id="value_one" /><br />
<input type="text" id="value_two" /><br />
<input type="text" id="total_value" /><br />

Sample jQuery

$("body").hover(function() {
    var a = +$('#value_one').val();
    var b = +$('#value_two').val();
    var total = a+b;
    $('#total_value').val(total);
});

fiddle

cfs
  • 10,610
  • 3
  • 30
  • 43
this_guy
  • 594
  • 1
  • 12
  • 24

1 Answers1

3

jQuery

$("#totalme").click(function () {
    var a = $('#value_one').val();
    var b = $('#value_two').val();
    var fltA = Number(a.replace(/[^0-9\.]+/g, ""));
    var fltB = Number(b.replace(/[^0-9\.]+/g, ""));
    var total = fltA + fltB;
    $('#total_value').val(total);
});

HTML

<input type="text" id="value_one" />
<br />
<input type="text" id="value_two" />
<br />
<input type="text" id="total_value" />
<br />
<button id="totalme">Total</button>

jsFiddle

DevlshOne
  • 8,357
  • 1
  • 29
  • 37