5 Replies - 565 Views - Last Post: 17 December 2012 - 04:34 PM

#1 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 385
  • View blog
  • Posts: 2,651
  • Joined: 12-January 10

Javascript not returning correct values

Posted 14 December 2012 - 08:48 AM

I am kinda hung over from holiday party last night and frankly cant think to straight.

I have the following javascript:



function add_repair_costs(x) {
  isMoneyRemove(x)

 

  var repair_cost = 0;


  if (document.getElementById('PS_FORM/REPAIR_ADDENDUM/Repair_Cost1').value) {
  if (isnumeric(document.getElementById('PS_FORM/REPAIR_ADDENDUM/Repair_Cost1').value)) {
  repair_cost = repair_cost + parseInt(document.getElementById('PS_FORM/REPAIR_ADDENDUM/Repair_Cost1').value);
  alert(repair_cost);
  }
  }

and so on


the call:

      $ <input type="text" style="width:100px;" value="{{PS_FORM/REPAIR_ADDENDUM/Repair_Cost1}}" name="PS_FORM/REPAIR_ADDENDUM/Repair_Cost1" id="PS_FORM/REPAIR_ADDENDUM/Repair_Cost1" onclick="add_repair_costs(this);" onblur="FormatMoney(this);" />


the format money is a function that puts commas and stuff into it.

so what it is doing is when i enter values into the input text boxes it formats it to money but my call has a function that removes the formating to do the math. However when it calls the add repair costs function if the value is in the thousands it will run on the intial on blur but if you goto the next input box the preceeding value only reads the number until it hits the "," in the input.


for instance:

input:                reads as:

1                       1
123                     123
1,234                   1234 on initial then back to 1


so if i have 3 input boxes it will do this:

input 1:   1
input 2:   1,234
input 3:   123

will read as follows:

1
1234
123


then when you click off of input 3

1
1
123



it does that after every thousands input on various input locations

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript not returning correct values

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,663
  • Joined: 30-January 09

Re: Javascript not returning correct values

Posted 14 December 2012 - 03:00 PM

Some locales use periods for the decimal point, some use commas. I'd say that's why the parseInt can't deal with it, it simply doesn't have those smarts.

Have you tried using RegEx to remove the commas?
repair_cost = repair_cost.replace(/\,/g,'');


Was This Post Helpful? 0
  • +
  • -

#3 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 385
  • View blog
  • Posts: 2,651
  • Joined: 12-January 10

Re: Javascript not returning correct values

Posted 17 December 2012 - 07:36 AM

that is what isMoneyRemove(x)is for.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2876
  • View blog
  • Posts: 9,544
  • Joined: 12-December 12

Re: Javascript not returning correct values

Posted 17 December 2012 - 12:47 PM

View PostDarenR, on 17 December 2012 - 07:36 AM, said:

that is what isMoneyRemove(x)is for.

Primitive values (strings, numbers, booleans) are passed by value to a function. That is, your function isMoneyRemove(x) will not change the value of x - so the commas will remain. Your function should work like this:

x = isMoneyRemove(x);

and the function should return the revised value of x.
Was This Post Helpful? 1
  • +
  • -

#5 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 385
  • View blog
  • Posts: 2,651
  • Joined: 12-January 10

Re: Javascript not returning correct values

Posted 17 December 2012 - 01:31 PM

I found my error

since I have so many if else in there i need to cycle the function in each if statement.


 if (document.getElementById('PS_FORM/REPAIR_ADDENDUM/Repair_Cost1').value) {  

10   if (isnumeric(document.getElementById('PS_FORM/REPAIR_ADDENDUM/Repair_Cost1').value)) {  

[quote]isMoneyRemove(x)[/quote]--- must be here



11   repair_cost = repair_cost + parseInt(document.getElementById('PS_FORM/REPAIR_ADDENDUM/Repair_Cost1').value);  

12   
13   }  

14   }  


Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Javascript not returning correct values

Posted 17 December 2012 - 04:34 PM

Use a var, make your code shorter and faster:
var value = document.getElementById('PS_FORM/REPAIR_ADDENDUM/Repair_Cost1').value;
if (value && isnumeric(value)) {  
	repair_cost +=  parseInt(value);
}  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1