Skip to main content
Topic: How to calculate and assign values using ClientEvents (Read 2678 times) previous topic - next topic

How to calculate and assign values using ClientEvents

Simple question, hard to receive an answer (by myself):
1) I'm in an ADD Form. I have 3 Fields:
F1: Quantity - initialized with 3
F2: Unit price - initialized with 10
F3: Total price => should be calculated ON-Change event for any field F1 or F2 (e.g. if I change Quantity => Total price should be recalculated).

2) How can I read Value of F1 if I change value of F2 ? or viceversa: How can I read Value of F2 if I change value of F1 ? ...in order to calculate and assign the value for F3 using ClientEvents (or somehow else) ?
Thanks for any reply ! Catalin

Re: How to calculate and assign values using ClientEvents

Reply #1
@Catalin‍ a sample code for reading values has been provided in phprad, the previous Ajax post solution you requested has a sample code for reading value from a field. You can also double click the sample codes on the left panel in ClientEvents.

Code: [Select]
$('the id of the field you want to read its value here').val();
then you can use it to do calculations.

Assigning value is as simple as below.
Code: [Select]
$('the id of the field you want to assign the value here').val('pass the value here');

Re: How to calculate and assign values using ClientEvents

Reply #2
Thanks for answering. is what I did many times before asking. Maybe because I used "ctrl-FIELD_Name" instead of "FIELD_Name" as you suggested ?! I will try anyway again ! Thanks for help !

Re: How to calculate and assign values using ClientEvents

Reply #3
Many thanks Willvin: worked for me, amazing !
My code bellow, as inspiration for others:

$('#ctrl-Pret_unitar_fara_TVA').on('change', function(){
     //initialize variables with fields values
   var pretUnitar = $(this).val(); //current field where event take place
   var Numar_de_luni = $('#ctrl-Numar_de_luni').val(); (pay attention: Database field name is Numar_de_luni)
   var Cantitate = $('#ctrl-Cantitate').val();
   //assignment of calculation to another field   
        $('#ctrl-Valoare_totala_fara_TVA').val(pretUnitar * Numar_de_luni * Cantitate);
});

Re: How to calculate and assign values using ClientEvents

Reply #4
Many thanks Willvin: worked for me, amazing !
My code bellow, as inspiration for others:

$('#ctrl-Pret_unitar_fara_TVA').on('change', function(){
     //initialize variables with fields values
   var pretUnitar = $(this).val(); //current field where event take place
   var Numar_de_luni = $('#ctrl-Numar_de_luni').val(); (pay attention: Database field name is Numar_de_luni)
   var Cantitate = $('#ctrl-Cantitate').val();
   //assignment of calculation to another field   
        $('#ctrl-Valoare_totala_fara_TVA').val(pretUnitar * Numar_de_luni * Cantitate);
});


not working on MODAL, or any idea please? Thanks in advance