Skip Navigation

[Resolved] Multiply two custom fields IN A VIEW LOOP | CRED form

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.65 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by Lara 3 years, 10 months ago.

Assisted by: Jamal.

Author
Posts
#1677737

Tell us what you are trying to do?
I need to multiply the value of two custom fields with JavaScript and display the result in a third custom field in a CRED EDIT form.
I am using the JavaScript:

jQuery( function( $ ) {
   
$('input[name=menge]').change(compute);
$('input[name=produktpreis]').change(compute);
   
function compute() {
      
        var a = $('input[name=menge]').val();
        var b = $('input[name=produktpreis]').val();
        var gesamtpreis = a * b;
 
        $('input[name=gesamtpreis]').val(gesamtpreis);

}
   
} );

It works awesome as long as I use the javascript code on a single page. As soon as I use it inside a view loop, it will start to use for "var a" and "var b" the values of the custom fields in the first loop item. Is there a way, that I can give the custom fields a loop index or a post ID. For example like name=menge_loop-number. I did search for something like that here in the support forum, but I couldn't find a hint or an idea.. how can this be approached in the best way.

Is there any documentation that you are following?
https://toolset.com/forums/topic/multiply-two-custom-fields-cred-form/

Is there a similar example that we can see?
no

What is the link to your site?
hidden link

#1678133

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

This code, in fact, will always look for the first inputs that match the selectors 'input[name=menge]' and 'input[name=produktpreis]'.
The compute function needs to be adapted to look for the input in the same form. The following code may work for you, I did not test it:

function compute() {
       var form = jQuery(this).closest("form");
        var a = form.find('input[name=menge]').val();
        var b = form.find('input[name=produktpreis]').val();
        var gesamtpreis = a * b;
  
        form.find('input[name=gesamtpreis]').val(gesamtpreis);
}

If this code does not work for you, please allow me temporary access to your site and let me check it closely, I'll need to know what form and what view are involved. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1681055

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.