Forums

value in field A is 4% up in filed B - Load JS problem

Repitol 08 Mar, 2015
Hi,
I use Load JS action to calculate prices for different activities
at the end, the total price is automatically filled
I need a new field for paypal payment that displays the total + 4%
but I don't find in the forums how to edit the corresponding Load JS Action
I've tried something but as I don't know JS coding....
window.addEvent('domready', function() {
$('total_a_payer').addEvent('keyup', rekenen2);
});
function rekenen2() {
$('total_paypal').value = parseInt($('total_a_payer').value + (4/100));
}
Repitol 08 Mar, 2015
I have changed the code and its better, but the result is not automatically displayed and wrong because no decimals
(eg : it shows 12 in place of 12.45 and I have to use tab key for the field is working)
window.addEvent('domready', function() {
$('total_a_payer').addEvent('keyup', rekenen2);
});

function rekenen2() {
$('total_paypal').value = parseInt($('total_a_payer').value * (4/100)) + parseInt($('total_a_payer').value);
}


I'm close to the solution I think but need help. :?
Regards
Repitol 08 Mar, 2015
Looking for solutions, I've seen on JS forums that "parseInt" JS code displays only entire number.
so I tried to use "parseFloat" but results seem to be wrong and there are many decimal points.
I can't find a way out.
here is the complete JS code I use - Maybe you can find what's wrong ? It would be so great.

window.addEvent('domready', function() {
$('total_soiree_restaurant_adultes').addEvent('keyup', rekenen1);
$('total_restaurant_enfants').addEvent('keyup', rekenen1);
$('total_coupe_ski_adultes').addEvent('keyup', rekenen1);
$('total_coupe_ski_enfants').addEvent('keyup', rekenen1);
$('total_ski_seul_adultes').addEvent('keyup', rekenen1);
$('total_ski_seul_enfants').addEvent('keyup', rekenen1);
$('total_buffet_adultes').addEvent('keyup', rekenen1);
$('total_buffet_enfants').addEvent('keyup', rekenen1);
$('total_soiree_restau_adultes').addEvent('keyup', rekenen1);
$('total_soiree_restau_enfants').addEvent('keyup', rekenen1);
$('total_ski_libre').addEvent('keyup', rekenen1);
});
function rekenen1() {
$('total_a_payer').value = parseInt($('total_soiree_restaurant_adultes').value * 55) +
parseInt($('total_restaurant_enfants').value * 25) +
parseFloat($('total_coupe_ski_adultes').value * 65.60) +
parseFloat($('total_coupe_ski_enfants').value * 58.80) +
parseFloat($('total_ski_seul_adultes').value * 48.60) +
parseFloat($('total_ski_seul_enfants').value * 41.80) +
parseInt($('total_buffet_adultes').value * 0) +
parseInt($('total_buffet_enfants').value * 0) +
parseInt($('total_soiree_restau_adultes').value * 60) + 
parseInt($('total_soiree_restau_enfants').value * 25) +
parseInt($('total_ski_libre').value * 22);
}
window.addEvent('domready', function() {
$('total_a_payer').addEvent('keyup', rekenen2);
});
function rekenen2() {
$('total_paypal').value = parseFloat($('total_a_payer').value * (4/100)) + parseFloat($('total_a_payer').value);
}

Thanks
GreyHead 09 Mar, 2015
HI Repitol,

I would apply the ParseInt() or parseFloat() to the form value before the calculation rather than parsing the result of the calculation. e.g.
parseInt($('some_input')) * a * b
instead of
parseInt($('some_input') * a * b)
Notice where the last ) is in each case.

Bob
Repitol 09 Mar, 2015
Hi Bob,
I'm not sure I did the right thing because the result is NaN
can you have a glance at what I wrote ?
window.addEvent('domready', function() {
$('total_soiree_restaurant_adultes').addEvent('keyup', rekenen1);
$('total_restaurant_enfants').addEvent('keyup', rekenen1);
$('total_coupe_ski_adultes').addEvent('keyup', rekenen1);
$('total_coupe_ski_enfants').addEvent('keyup', rekenen1);
$('total_ski_seul_adultes').addEvent('keyup', rekenen1);
$('total_ski_seul_enfants').addEvent('keyup', rekenen1);
$('total_buffet_adultes').addEvent('keyup', rekenen1);
$('total_buffet_enfants').addEvent('keyup', rekenen1);
$('total_soiree_restau_adultes').addEvent('keyup', rekenen1);
$('total_soiree_restau_enfants').addEvent('keyup', rekenen1);
$('total_ski_libre').addEvent('keyup', rekenen1);
});
function rekenen1() {
$('total_a_payer').value = parseInt($('total_soiree_restaurant_adultes')).value * 55 +
parseInt($('total_restaurant_enfants')).value * 25 +
parseFloat($('total_coupe_ski_adultes')).value * 65.60 +
parseFloat($('total_coupe_ski_enfants')).value * 58.80 +
parseFloat($('total_ski_seul_adultes')).value * 48.60 +
parseFloat($('total_ski_seul_enfants')).value * 41.80 +
parseInt($('total_buffet_adultes')).value * 0 +
parseInt($('total_buffet_enfants')).value * 0 +
parseInt($('total_soiree_restau_adultes')).value * 60 + 
parseInt($('total_soiree_restau_enfants')).value * 25 +
parseInt($('total_ski_libre')).value * 22;

}
window.addEvent('domready', function() {
$('total_a_payer').addEvent('keyup', rekenen2);
});

function rekenen2() {
$('total_paypal').value = parseFloat($('total_a_payer')).value * (4/100) + parseFloat($('total_a_payer')).value;
}
GreyHead 10 Mar, 2015
Hi Repitol,

The JavaScript looks OK to me - but you will get an NaN error if any of the input values is not set - do they default to 0 (zero)? If not your code will need to check that they value is set, and set a zero value if it isn't.
function rekenen1() {
  var total = 0;
  if ( isNaN($('total_soiree_restaurant_adultes')).value) === false ) {
    total =+ $('total_soiree_restaurant_adultes')).value;
  }
  if ( isNaN($('total_restaurant_enfants')).value) === false ) {
    total =+ $('total_restaurant_enfants')).value;
  }
  if ( isNaN($('total_coupe_ski_adultes')).value) === false ) {
    total =+ $('total_coupe_ski_adultes')).value;
  }
  if ( isNaN($('total_coupe_ski_enfants')).value) === false ) {
    total =+ $('total_coupe_ski_enfants')).value;
  }
  if ( isNaN($('total_ski_seul_adultes')).value) === false ) {
    total =+ $('total_ski_seul_adultes')).value;
  }
  if ( isNaN($('total_ski_seul_enfants')).value) === false ) {
    total =+ $('total_ski_seul_enfants')).value;
  }
  if ( isNaN($('total_buffet_adultes')).value) === false ) {
    total =+ $('total_buffet_adultes')).value;
  }
  if ( isNaN($('total_buffet_enfants')).value) === false ) {
    total =+ $('total_buffet_enfants')).value;
  }
  if ( isNaN($('total_soiree_restau_adultes')).value) === false ) {
    total =+ $('total_soiree_restau_adultes')).value;
  }
  if ( isNaN($('total_soiree_restau_enfants')).value) === false ) {
    total =+ $('total_soiree_restau_enfants')).value;
  }
  if ( isNaN($('total_ski_libre')).value) === false ) {
    total =+ $('total_ski_libre')).value;
  }

  $('total_a_payer').value = total;
}

Bob
Repitol 10 Mar, 2015
Thanks for your reply Bob,
to go faster, the best is to set defaut value 0 for each field in the form right ?
doing that I would normally have the form working ?
because for the moment I still have NaN with every field set (values 1 or 2)

Repitol
Repitol 10 Mar, 2015
No way 😟
I've set default field value to 0 for each input but still have NaN
the form calculates only if I use code this way (but result is wrong because decimals are not taken into account)
parseFloat($('total_ski_seul_adultes').value * 48.60) +

and fails NaN when I set yours
parseFloat($('total_ski_seul_adultes')).value * 48.60 +


I try ideas but without knowledges that looks hazardious
GreyHead 10 Mar, 2015
Hi Repitol,

Yes I think so - try it and see.

Bob
Repitol 10 Mar, 2015
Ok so
parseFloat($('total_ski_seul_adultes')).value * 48.60 + 

never works for me -> only NaN result whatever I set in fields (0 or ther values)
parseFloat($('total_ski_seul_adultes').value * 48.60) + 

works but I have too many decimals. (ex : 65.6 + 58.8 = 124.39999999999999)
I would need just a fix to have only 2 decimals (and a round - 124.39999 should give 124.40).
I've seen threads about toFixed(2) but don't know how to apply that in my code
every "hazardious" attempts failed.
😟 😟
GreyHead 10 Mar, 2015
Hi Repitol,

This code
parseFloat($('total_ski_seul_adultes')).value
should be
parseFloat($('total_ski_seul_adultes').value)
with the ) after the ,value

Please see the JavaScript .toFixed() method to convert a float to a fixed decimal.

Bob
Repitol 10 Mar, 2015
Bob, I've found something new with my hazardious tests.
My result is ok and only 2 decimals with the following code (I added parentesis at the beginning and at the end of the total_a_payer code + added .toFixed(2) )
My last problem is with the second "total_paypal". It is automatically OK only if I write manually the result in the "total_a_payer" field
when the "total_a_payer" field is set automatically thanks to the code, "Total_a_payer" stays 0.
Repitol 10 Mar, 2015

Hi Repitol,

This code

parseFloat($('total_ski_seul_adultes')).value
should be
parseFloat($('total_ski_seul_adultes').value)
with the ) after the ,value

Please see the JavaScript .toFixed() method to convert a float to a fixed decimal.

Bob



Bob ! you told me to put the code that way on your previous reply didn't you ? 🙄

HI Repitol,

I would apply the ParseInt() or parseFloat() to the form value before the calculation rather than parsing the result of the calculation. e.g.

parseInt($('some_input')) * a * b

instead of

parseInt($('some_input') * a * b)

Notice where the last ) is in each case.

Bob

GreyHead 10 Mar, 2015
Hi Repitol,

Yes sorry, I missed the .value out :-(

Bob
Repitol 10 Mar, 2015
No problem but can you see my previous post "Bob, I've found something new with my hazardious tests." ?
I miss a last point to have total_paypal working correctly
After that, I will be able to put the form online as my customer urge me to do.
;)
GreyHead 10 Mar, 2015
Hi Repitol,

Presumably there is a bug in your code . . . what is the code that you are now using?

Bob
Repitol 10 Mar, 2015
this one
window.addEvent('domready', function() {
$('total_soiree_restaurant_adultes').addEvent('keyup', rekenen1);
$('total_restaurant_enfants').addEvent('keyup', rekenen1);
$('total_coupe_ski_adultes').addEvent('keyup', rekenen1);
$('total_coupe_ski_enfants').addEvent('keyup', rekenen1);
$('total_ski_seul_adultes').addEvent('keyup', rekenen1);
$('total_ski_seul_enfants').addEvent('keyup', rekenen1);
$('total_soiree_restau_adultes').addEvent('keyup', rekenen1);
$('total_soiree_restau_enfants').addEvent('keyup', rekenen1);
$('nuitsingle').addEvent('keyup', rekenen1);
$('nuitdouble').addEvent('keyup', rekenen1);
$('nuitdouble').addEvent('keyup', rekenen1);
$('nuittriple').addEvent('keyup', rekenen1);
$('nuitquadruple').addEvent('keyup', rekenen1);
});
function rekenen1() {
$('total_a_payer').value = parseFloat(($('total_soiree_restaurant_adultes').value * 55) +
parseFloat($('total_restaurant_enfants').value * 25) +
parseFloat($('total_coupe_ski_adultes').value * 65.60) +
parseFloat($('total_coupe_ski_enfants').value * 58.80) +
parseFloat($('total_ski_seul_adultes').value * 48.60) +
parseFloat($('total_ski_seul_enfants').value * 41.80) +
parseFloat($('total_soiree_restau_adultes').value * 60) + 
parseFloat($('total_soiree_restau_enfants').value * 25) +
parseFloat($('nuitsingle').value * 184.25) +
parseFloat($('nuitdouble').value * 198.50) +
parseFloat($('nuittriple').value * 231.75) +
parseFloat($('nuitquadruple').value * 261)).toFixed(2);
}
window.addEvent('domready', function() {
$('total_a_payer').addEvent('keyup', rekenen2);
});
function rekenen2() {
$('total_paypal').value = parseFloat(($('total_a_payer').value * (4/100)) + parseFloat($('total_a_payer').value)).toFixed(2);
}
GreyHead 10 Mar, 2015
Hi Repitol,

The rekenen2 function is called on keyUp in the total_a_payer box so it will only run then. You might try the change event instead
$('total_a_payer').addEvent('change', rekenen2);

Bob
Repitol 10 Mar, 2015
unfortunately same problem with "change"
GreyHead 10 Mar, 2015
HI Repitol,

Then why not just add it at the end of the first function. That is when you want it to run.

Bob
Repitol 10 Mar, 2015
Huhhhh
like ?
GreyHead 10 Mar, 2015
1 Likes
Hi Repitol,

window.addEvent('domready', function() {
$('total_soiree_restaurant_adultes').addEvent('keyup', rekenen1);
$('total_restaurant_enfants').addEvent('keyup', rekenen1);
$('total_coupe_ski_adultes').addEvent('keyup', rekenen1);
$('total_coupe_ski_enfants').addEvent('keyup', rekenen1);
$('total_ski_seul_adultes').addEvent('keyup', rekenen1);
$('total_ski_seul_enfants').addEvent('keyup', rekenen1);
$('total_soiree_restau_adultes').addEvent('keyup', rekenen1);
$('total_soiree_restau_enfants').addEvent('keyup', rekenen1);
$('nuitsingle').addEvent('keyup', rekenen1);
$('nuitdouble').addEvent('keyup', rekenen1);
$('nuitdouble').addEvent('keyup', rekenen1);
$('nuittriple').addEvent('keyup', rekenen1);
$('nuitquadruple').addEvent('keyup', rekenen1);
});

function rekenen1() {
  $('total_a_payer').value = parseFloat(($('total_soiree_restaurant_adultes').value * 55) +
    parseFloat($('total_restaurant_enfants').value * 25) +
    parseFloat($('total_coupe_ski_adultes').value * 65.60) +
    parseFloat($('total_coupe_ski_enfants').value * 58.80) +
    parseFloat($('total_ski_seul_adultes').value * 48.60) +
    parseFloat($('total_ski_seul_enfants').value * 41.80) +
    parseFloat($('total_soiree_restau_adultes').value * 60) + 
    parseFloat($('total_soiree_restau_enfants').value * 25) +
    parseFloat($('nuitsingle').value * 184.25) +
    parseFloat($('nuitdouble').value * 198.50) +
    parseFloat($('nuittriple').value * 231.75) +
    parseFloat($('nuitquadruple').value * 261)).toFixed(2);
  $('total_paypal').value = parseFloat(($('total_a_payer').value * (4/100)) + parseFloat($('total_a_payer').value)).toFixed(2);
}


Bob
Repitol 10 Mar, 2015
Ok Ok OK !!!
I get you !
it works now
the code is
window.addEvent('domready', function() {
$('total_soiree_restaurant_adultes').addEvent('keyup', rekenen1);
$('total_restaurant_enfants').addEvent('keyup', rekenen1);
$('total_coupe_ski_adultes').addEvent('keyup', rekenen1);
$('total_coupe_ski_enfants').addEvent('keyup', rekenen1);
$('total_ski_seul_adultes').addEvent('keyup', rekenen1);
$('total_ski_seul_enfants').addEvent('keyup', rekenen1);
$('total_soiree_restau_adultes').addEvent('keyup', rekenen1);
$('total_soiree_restau_enfants').addEvent('keyup', rekenen1);
$('nuitsingle').addEvent('keyup', rekenen1);
$('nuitdouble').addEvent('keyup', rekenen1);
$('nuitdouble').addEvent('keyup', rekenen1);
$('nuittriple').addEvent('keyup', rekenen1);
$('nuitquadruple').addEvent('keyup', rekenen1);
});
function rekenen1() {
$('total_a_payer').value = parseFloat(($('total_soiree_restaurant_adultes').value * 55) +
parseFloat($('total_restaurant_enfants').value * 25) +
parseFloat($('total_coupe_ski_adultes').value * 65.60) +
parseFloat($('total_coupe_ski_enfants').value * 58.80) +
parseFloat($('total_ski_seul_adultes').value * 48.60) +
parseFloat($('total_ski_seul_enfants').value * 41.80) +
parseFloat($('total_soiree_restau_adultes').value * 60) + 
parseFloat($('total_soiree_restau_enfants').value * 25) +
parseFloat($('nuitsingle').value * 184.25) +
parseFloat($('nuitdouble').value * 198.50) +
parseFloat($('nuittriple').value * 231.75) +
parseFloat($('nuitquadruple').value * 261)).toFixed(2);
$('total_paypal').value = parseFloat(($('total_a_payer').value * (4/100)) + parseFloat($('total_a_payer').value)).toFixed(2);
}

Thank you so much Bob !
This topic is locked and no more replies can be posted.