Calculate multiple fields

  • Ckeyboard1
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 18
  • Thank you received: 0

Ckeyboard1 created the topic: Calculate multiple fields

I'm trying to add or subtract multiple fields. Fields are optional but I am using a radio button to choose if it adds or subtracts. So looks like this.

Bob has 10 apples

Bob adds 3 apples

then Bob eats 5 apples

Or Bob can add or subtracts apples but he doesn't have to.

I know how to code the calculations but think I have to have if or else statements. Think I have to tell the total field what the value of my radio button is and to either add or subtract. What would that code look like?
#233548
  • Posts: 2483
  • Karma: 86
  • Thank you received: 335

mihaela replied the topic: Calculate multiple fields

Hello,

Yes, you are right, if statements are needed in your case. Here is an example code where field values are added or subtracted depending on radio group selected option:
if(jQuery('[name="ff_nm_radiogroupname[]"]:checked').val()  == "add") {
       ff_getElementByName('result').value = 
       Number(ff_getElementByName('field1').value) + 
       Number(ff_getElementByName('field2').value);
}
else if(jQuery('[name="ff_nm_radiogroupname[]"]:checked').val()  == "subtract") {
       ff_getElementByName('result').value = 
       Number(ff_getElementByName('field1').value)- 
       Number(ff_getElementByName('field2').value);
}

NOTE: In the code above you have to replace each instance of radiogroupname with the Name of the radio group element.
Also, replace result, field1 and field2 with the Names of the elements you want to calculate with.

Let me know whether this helped and whether you've managed to write the code to that form works as you wanted.

Regards,

Mihaela
#233552
  • Ckeyboard1
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 18
  • Thank you received: 0

Ckeyboard1 replied the topic: Calculate multiple fields

Ok, So I put in the code but I think I'm missing some things. Here is my fields.

InitialCostValue (This is the number user inputs)
CurrentCostValue (This is the total field)
increaseDecrease1 (This is the radio group that user selects to Increase (add) or Decrease (Subtract)

0;Increase;Increase
0;Decrease;Decrease
0;NA;NA


TotalIncrease1 (This is the field the user inputs if the cost increase) (add) or
TotalDecrease1 (This is the field the user input a decrease ) (Subtracts)

So in the advanced tab, I clicked Custom and input this code you wrote and it looks like this.

if(jQuery('[name="ff_nm_increaseDecrease1[]"]:checked').val() == "Increase") {
ff_getElementByName('CurrentCostValue').value =
Number(ff_getElementByName('InitialCostValue').value) +
Number(ff_getElementByName('TotalIncrease1').value);
}
else if(jQuery('[name="ff_nm_increaseDecrease1[]"]:checked').val() == "Decrease") {
ff_getElementByName('CurrentCostValue').value =
Number(ff_getElementByName('InitialCostValue').value)-
Number(ff_getElementByName('TotalDecrease1').value);
}

I put the code in each filed. InitialCostValue, CurrentCostValue, and TotalIncrease1, and TotalIncrease2

With all that in mind, what did I do wrong?


It's also important to note, that I all have to add multiple fields where the cost can change.

Example: TotalIncrease1, TotalIncrease2, TotalIncrease3 and so one. Same for Decrease.

I hope this all makes sense. Simple adding and subtraction, difficult and very complicated.
#233580
  • Ckeyboard1
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 18
  • Thank you received: 0

Ckeyboard1 replied the topic: Calculate multiple fields

I put in this code.

if(jQuery('[name="ff_nm_increaseDecrease1[]"]:checked').val() == "Increase") {
ff_getElementByName('CurrentCostValue').value =
Number(ff_getElementByName('InitialCostValue').value) +
Number(ff_getElementByName('TotalIncrease1').value);
}
else if(jQuery('[name="ff_nm_increaseDecrease1[]"]:checked').val() == "Decrease") {
ff_getElementByName('CurrentCostValue').value =
Number(ff_getElementByName('InitialCostValue').value)-
Number(ff_getElementByName('TotalDecrease1').value);
}

I put this in CurrentCostValue, InitialCostValue, TotalDecrease1 and TotalIncrease1

The value of my radio group is this.
0;Increase;Increase
0;Decrease;Decrease
0;NA;NA

Name of my radio group button is increaseDecrease1
Name of field where user inputs value is InitialCostValue
Name of field where Total is calulated is CurrentCostValue

So calculation is InitialCostValue + TotalIncrease1 = CurrentCostValue

Not working and I know I'm not doing this right.
#233583
  • Posts: 2483
  • Karma: 86
  • Thank you received: 335

mihaela replied the topic: Calculate multiple fields

Hello,

I've tested the code you've written and for me it was working fine.

Therefore, I assume that you haven't put the code framework.
In the Actionscript the whole code has to look like the following:
function ff_elementname_action(element, action)
{
    if(jQuery('[name="ff_nm_increaseDecrease1[]"]:checked').val() == "Increase") {
ff_getElementByName('CurrentCostValue').value =
Number(ff_getElementByName('InitialCostValue').value) +
Number(ff_getElementByName('TotalIncrease1').value);
}
else if(jQuery('[name="ff_nm_increaseDecrease1[]"]:checked').val() == "Decrease") {
ff_getElementByName('CurrentCostValue').value =
Number(ff_getElementByName('InitialCostValue').value)-
Number(ff_getElementByName('TotalDecrease1').value);
}
} // ff_elementname_action
NOTE: In each Actionscript you need to replace elementname with the name of the element fro which this Actionscript, for example
function ff_InitialCostValue_action(element, action)
for InitialCostValue element.

Let me know whether it works as you wanted after you add the code as explained to Actionscripts.

Regards,

Mihaela
#233608
  • Ckeyboard1
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 18
  • Thank you received: 0

Ckeyboard1 replied the topic: Calculate multiple fields

I put the code in and still not working g so I am not putting this in correctly or in the wrong place.

I put the code in all 4 places in Action script.
InitialCostValue
CurrentCostValue
TotalIncrease1
TotalDecrease1

The Current cost filed should auto populate once I put in the Initialcost value.
#233620
Moderators: ForumSupporttomeperica
Time to create page: 0.069 seconds

New Icon Packs Category!

Crosstec is now offering icon packs.

If you are a paying subscriber, icon packs are automatically added to your account.

Check out our icon packs page!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Summer Sale!

50% discount on all of our extension subscription plans, templates and icon packs!

Get Your Subscription Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99 for just 49! (Summer Sale)

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here