numbers - Undesired effects when calculating 2 amounts with jQuery -


i not getting desired output code. seems working except if example enter between 100 - 199. gives negative number in other box, when split balance both boxes. can see working example here: http://jsfiddle.net/bfjq2/ ( enter in 100 see mean )

not sure if there doing wrong, feel treating number decimal or something. appreciated.

here js:

// make sure both inputs equal balance $('input[id*="amount"]').on("blur", function() {     var balance     = $('#balance').val()        ,thisamount  = $(this)        ,otheramount = $('input[id*="amount"]').not(this);      // check if value less balance     if(thisamount.val() < balance && thisamount.val() + otheramount.val() <= balance){         // if populate other input remaining amount         otheramount.val(balance - thisamount.val());     }      // check make sure amount not greater balance     if((thisamount.val() + otheramount.val()) > balance){         $('input[id*="amount"]').not(this).val(function(){             // if value greater balance, split balance both amounts             if(thisamount.val() > balance){                 thisamount.val(balance/2);                 return balance/2;             }         });     } }); 

your values being interpreted strings, not numbers. see happens when debug:

debug values

so comparisons string comparisons ("alphabetically", "100" before "20"). it's doing string concatenation when use + operator.

you need convert values numbers before use them, using number(), parsefloat(), or parseint(), depending.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -