Forums

Need little JS help :P

CrystalFrontier 28 Dec, 2013
Hello everyone

I could use a little line of JS (I presume) for what I believe may be only a little tweak for someone with more skills than mine😉

I have two dropdowns. One ('teams') has values 1,2 and 3. A second dropdown (here:'judges') offers values up to eight. Any 'judges' participating in the event pay 180 EUR. If, however, there are +1 more judges than teams they get a discount, so the part. fee is 160, not 180 for each of those judges. Likewise, if there are +2 or more than # of teams, the discount part. fee is 140€. There are no discounts if the # of judges are equal to the # of teams.

Example: one team brings 2 judges (n+1) - both judges pay 160, so the discount on the delegation is 2*20
Example: two teams bring 6 judges (n+2 or more) - all six pay 140, so the discount on the delegation is 6*40 EUR.
Example: three teams bring three judges - no discounts. All pay 180.

I've got the 'Prospective fees' textbox to show the values * 180 only, but they would be less, of course, if the values in 'judges' are +1 or +2 than the values in 'teams'.

I assume, this would demand a simple 'if-then' programming, of which I don't know what it should look like. Any help would be much appreciated.

Please check the following image for clarification. Here, there are five participants altogether, adding up to 900€ (5*180), but with two n+1 judges, it should be 860€ (3*180 + 2*160):



This is the JS event for the 'Prospective fee' box {total}:

window.addEvent('domready', function() {
  $('debaters').addEvent('change', rekenen1);
  $('judges').addEvent('change', rekenen1);
});
function rekenen1(){
  $('total').value = $('debaters').value * 180 + $('judges').value * 180;
}


Thanks and Happy New Year!
Chris
CrystalFrontier 29 Dec, 2013
Hello Sloan Thrasher

thanks so much for the speedy reply! I did make some changes and I think I now got it working the way I wanted.

window.addEvent('domready', function() {
   $('debaters').addEvent('change', rekenen1);
   $('judges').addEvent('change', rekenen1);
});
function rekenen1(){
   var num_teams = $('teams').value;
   var num_judges = $('judges').value;
   var num_extras = num_judges - num_teams;
   var discount = 0;
   if(num_extras > 0) {
      discount = 20;
      if(num_extras > 1) {
         discount = 40;
      }
   }
   $('total').value = $('debaters').value * 180 + $('judges').value * (180 - discount);
}


I had to change 'debaters' into 'teams' on "var num_teams", as this is the determining number from which the discount applies if there are >1 judges than teams (namely two in case of one team).

The "if(num_extras)" values should be > 0 and > 1, though, giving a discount to n+1 and n+2 respectively. The difference being only 1 after subtracting num_teams from num_judges giving the doscount of 20 and of 40 if the difference is >1, not 2😉

Example: One team, three debaters, two judges = 3*180 + 2*160 = 860 (not 900), the discount being 2*20 (20 per judge).

Anyways, thanks very much for your help! Much appreciated!!

See live page here if you want (work in progress):
http://www.schoolsdebate.org/index.php?option=com_chronoforms&chronoform=EurOpen_PRE&lang=en
Chris
CrystalFrontier 02 Jan, 2014
Hello again sloanthrasher,

I have a new fix I can't put my head around.

I would simply like to mirror all added total values in 'due_oct' (last field) minus the down payment (by 1st SEP), here: 180EUR.



The problem may be that all totals are already comprised by other 'rekenen' functions (rekenen1-4). This fifth JS should reflect the values from the three green fields miuns the one red field, namely

'due_oct' = 'total' + 'total_no' + 'total_local' - ('due')

This is the code I've used, but NOTHING is happening there.

window.addEvent('domready', function() {
   $('total').addEvent('change', rekenen5);
   $('total_no').addEvent('change', rekenen5);
   $('total_local').addEvent('change', rekenen5);
   $('due').addEvent('change', rekenen5);
});
function rekenen5(){
   $('due_oct').value = $('total').value + $('total_no').value + $('total_local').value - $('due').value;
}


I was, however, able to use the same JS for the 'due' field (upper red) to mirror the exact same function in 'due_oct'. However, once I change the parameters of the function, nothing happens.
It's possible that some typo sneaked in, but as I said, it's pretty much a copy/paste version of the first red-field function, which works just fine, so I may have to rule it out.

Where am I going wrong?

Thanks for any help😉
Chris
GreyHead 02 Jan, 2014
Hi Chris,

Can you post a link to the form page - that makes debugging much easier.

My guess is that your problem here is that + in JavaScript is ambiguous - it is used both for concatenation and for addition. You might need to add parseInt() or parseFloat() to force the values from strings (which will be concatenated) to numeric values.

You should also check that 'due_oct' has an Id set - your browser Console should show an error if it doesn't.

Bob
GreyHead 03 Jan, 2014
Hi Chris,

As I suspected the values get concatenated and you end up with a long string of numbers. Please try:
$('due_oct').value = $('total').value.toInt() + $('total_no').value.toInt() + $('total_local').value.toInt() - $('due').value.toInt()

Bob
CrystalFrontier 03 Jan, 2014
Hello Greyhead,

sorry, but there is still no response in the 'due_oct' target field, unless I've embedded it wrong?

window.addEvent('domready', function() {
   $('total').addEvent('change', rekenen5);
   $('total_no').addEvent('change', rekenen5);
   $('total_local').addEvent('change', rekenen5);
   $('due').addEvent('change', rekenen5);
});
function rekenen5(){
   $('due_oct').value = $('total').value.toInt() + $('total_no').value.toInt() + $('total_local').value.toInt() - $('due').value.toInt();
}


Thanks for your time!!
Chris
CrystalFrontier 04 Jan, 2014
Hello Sloanthrasher,

I have difficulties to understand this sentence:

Use the change event on the original fields to update all of the totals - that is, the original script in the thread.



Do you mean a Change event action? And if so, how can I place this 'on the original fields'?

Which 'original script in the thread' are you referring to?

As you can see, I have no clue of all these things, regrettably. :? Sorry for being such a pain.

All fields in question carry name and also id. I'm a fraid I lack the programming skills to just merge all functions into one, but I assume this is where the conflict arises.

Chris
This topic is locked and no more replies can be posted.