|SOLVED| multiplying and subtracting values on text fields.

Ok. hopefully I won’t confuse anyone. I had code that worked fine. Just not on the Safari browser. I was told Safari doen’t like Math code. See code below…

$w . onReady ( function () {
$w ( ‘#input247’ ). onChange (( Event ) => {
const salesPrice = Math . trunc ( Number ( $w ( ‘#input247’ ). value )) ;
const stdCommission = Math . round ( salesPrice * 0.06 );
const ourCommission = Math . round ( salesPrice * 0.025 );
const savings = stdCommission - ourCommission ;

    $w ( '#input247' ). value  =  **null** ; 

    $w ( "#text548" ). text  =  '$'  +  salesPrice . toLocaleString (); 
    $w ( "#text549" ). text  =  '$'  +  stdCommission . toLocaleString (); 
    $w ( "#text550" ). text  =  '$'  +  ourCommission . toLocaleString (); 
    $w ( "#text551" ). text  =  '$'  +  savings . toLocaleString (); 
   })  

});

I changed the code but the last line (subtraction) is returning a $NaN value. I highlighted problem line code in yellow. See code below…

$w . onReady ( function () {
$w ( “#input247” ). onChange (( Event ) => {

**let**  price  =  Number ( $w ( '#input247' ). value ); 
**let**  traditionalPercent  =  Number  ( $w ( '#text557' ). text ); 
**let**  buyersAgentPercent  =  Number  ( $w ( '#text560' ). text ); 
**let**  traditionalTotal  =  Number  ( $w ( '#text549' ). text ); 
**let**  buyersAgentTotal  =  Number  ( $w ( '#text550' ). text ); 

$w ( "#input247" ). value  =  **null** ; 

$w ( '#text548' ). text  =  '$'  +  price . toLocaleString (); 
$w ( '#text549' ). text  =  '$'  + ( price  *  t raditionalPercent ). toLocaleString (); 
$w ( '#text550' ). text  =  '$'  + ( price  *  buyersAgentPercent ). toLocaleString (); 
$w ( '#text551' ). text  =  '$'  + ( traditionalTotal  -  buyersAgentTotal ). toLocaleString (); 

})
});

Can anyone please help with that last line.

Looks like either traditionalTotal or buyersAgentTotal are not valid numbers. Have you tried to console.log them before doing the subtraction?

No. I’m a a novice a coding. I did research and came up with that code. I was surprised the multiplication code worked at all. It’s just the subtraction line.

I have no idea what to look for in the console log. I opened it and didn’t know what I was looking at.

First, I’m not aware of any issue with Math in Safari - I use both.

I would suggest that you use onInput() instead of onChange() - simply put, onInput() works better.

I figured it out. Took a while but I did it. Code below now works on iPhone/Safari perfectly.

$w . onReady ( function () {
$w ( “#input247” ). onChange (( Event ) => {

**let**  price  =  Number ( $w ( '#input247' ). value ); 
**let**  traditionalPercent  =  Number  ( $w ( '#text556' ). text ); 
**let**  buyersAgentPercent  =  Number  ( $w ( '#text557' ). text ); 
 

$w ( "#input247" ). value  =  **null** ; 

$w ( '#text548' ). text  =  '$'  +  price . toLocaleString (); 
$w ( '#text549' ). text  =  '$'  + ( price  *  traditionalPercent ). toLocaleString (); 
$w ( '#text550' ). text  =  '$'  + ( price  *  buyersAgentPercent ). toLocaleString (); 
$w ( '#text551' ). text  =  '$'  + ( price  *  traditionalPercent  -  price  *  buyersAgentPercent ). toLocaleString (); 

})
});