BF : Radio button groups and calculation

  • Gilles361
  • Topic Author
  • Offline
  • Gold Boarder
  • Gold Boarder
  • Posts: 174
  • Thank you received: 5

Gilles361 created the topic: BF : Radio button groups and calculation

Hi,

I've defined two radio button groups : Nb1 & Nb2
I would like to calculate a hidden element 'Montant' = Nb1 * 5 + Nb2 *2
Then display all elements in a summary Page (OK)

I can't figure out to get calculation of 'Montant' hidden element ?
Montant is all the time empty
Nb1 & Nb2 OK

I've attached my "GN test form"

My "Montant" script doesn't work...
function ff_Montant_action(element, action) {
ff_getElementByName('Montant').value =
Number(ff_getElementByName('nb1').value) * 5 +
Number(ff_getElementByName('nb2').value) * 2;
} // ff_Montant_action

Thanks for help
#236443
Attachments:
  • Posts: 2359
  • Karma: 85
  • Thank you received: 316

mihaela replied the topic: BF : Radio button groups and calculation

Hello,

Since Nb1 and Nb2 elements are radio group elements, their selected value can't be fetched by ff_getElementByName('nb1').value. Instead, you should use this function to fetch the value:
jQuery('[name="ff_nm_nb1[]"]:checked').val()

That should resolve the issue. Let me know whether it worked after the change.

Regards,

Mihaela
#236453
  • Gilles361
  • Topic Author
  • Offline
  • Gold Boarder
  • Gold Boarder
  • Posts: 174
  • Thank you received: 5

Gilles361 replied the topic: BF : Radio button groups and calculation

Hi Mihaela,

I've simplified my hidden element, custom / page - Initscript, to:

function ff_Montant_action(element, action) {
ff_getElementByName('Montant').value =
jQuery('[name="ff_nm_nb1[]"]:checked').val() ;
} // ff_Montant_action

'Montant' (Empty!) is not set up to nb1?

Any idea?
Is ff_elementname_action (element, action) is OK?

Thks, Gil
#236484
  • Gilles361
  • Topic Author
  • Offline
  • Gold Boarder
  • Gold Boarder
  • Posts: 174
  • Thank you received: 5

Gilles361 replied the topic: BF : Radio button groups and calculation

Hi Mihaela,
Thanks for radio tip, initscipt bellow does the job.
Also, I had : function ff_ElementName_action() rather than function ff_ElementName_init()

function ff_Montant_init() {
ff_getElementByName('Montant').value =
Number(jQuery('[name="ff_nm_nb1[]"]:checked').val() ) * 5 +
Number(jQuery('[name="ff_nm_nb2[]"]:checked').val() ) * 2 ;
} // function ff_Montant_init

Thanks, Gil
#236486
  • Posts: 2359
  • Karma: 85
  • Thank you received: 316

mihaela replied the topic: BF : Radio button groups and calculation

Hello,

Here is an explanation of why this didn't work for you:
function ff_Montant_action(element, action) {
ff_getElementByName('Montant').value =
jQuery('[name="ff_nm_nb1[]"]:checked').val() ;
} // ff_Montant_action

The thing is that the Actionscript code is run when a certain action is done. For example, if you check "Change" as action for Actionscript in the "Montant" element then the script will be run after you, for example, write something in the "Montant" field.

This is not how you want this to work. Therefore, you should write Actionscripts for the "nb1" and "nb2" elements since you wish the scripts to be run when change is made in those fields.
Please take a look at this crosstec.org/en/support/online-documenta...ting-textfields.html . Here it is explained how to set the calculation.

Another way you could write the calculation using one snippet of code instead of writing Actionscript for every element included in the calculation is to write Init script like this:
function ff_Montant_init() {
    setInterval(calcul, 500);
} // function ff_Montant_init

function calcul(){
    ff_getElementByName('Montant').value =
    Number(jQuery('[name="ff_nm_nb1[]"]:checked').val() ) * 5 +
    Number(jQuery('[name="ff_nm_nb2[]"]:checked').val() ) * 2 ;
}
The script that you have used will do the calculation when the page is entered and if value in "nb1" or "nb2" is changed afterwards, the new calculations will not be done.

Let me know whether this is working as you wanted.

Regards,

Mihaela
#236491
The following user(s) said Thank You: MarioPuco
  • Gilles361
  • Topic Author
  • Offline
  • Gold Boarder
  • Gold Boarder
  • Posts: 174
  • Thank you received: 5

Gilles361 replied the topic: BF : Radio button groups and calculation

Thanks Mihaela,
I will try your "interval" trick.
My code works as I've added a PREV/NEXT Button... to fetch my values.
BR, Gil
#236639
Moderators: ForumSupporttomeperica
Time to create page: 0.070 seconds

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!

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