Jump to content

how to define 2 total virtual fields calculation in 1 javascript in 1 datapa

Recommended Posts


I am stuck with javascript doing multiple calculations in same datapage.

  • 1st javascript is calculation of  unit cost, no of units and total (in local currency)
  • 2nd javascript is convert local unit cost to USD based on cascading dropdown field
  • 3rd javascript is using unit cost (in USD), no of units to derive total cost (in USD)


the 1st javascript seems to be working. But when I add on 2nd javascript, only one value shows and in the wrong field. There is also no error appearing in the web console.


  • javascript 1 calculation = virtual field 1 = virtual field 1 value
  • javascript 2 calculation = virtual field 2 = virtual field 2 value


Instead what i see is this

  • javascript 1 = virtual field 1 = virtual field 2 value
  • javascript 2 = virtual field 2 = blank


I followed basically this script and did my own modification http://forums.caspio.com/index.php?/topic/6053-perform-calculations-in-submission-form/

I think the problem is I did not define 2 variable that points to virtual field 1 and virtual field 2 respectively.

 var totalField  = document.getElementsByClassName('cbFormData')[0];

I need 2 of it, and I do not how to define for the 2nd virtual field.


Also, how do I pass the the virtual field to a table field? I used the  Pass field value as parameter, it does not seem to write into the table.

Can anyone please help?


<script type="text/javascript">
    // Caspio form elements to calculate local total cost
    var unitcostField = document.getElementById('InsertRecordLocalUnitCost');
    var unitsField   = document.getElementById('InsertRecordUnit');
   // var totalField  = document.getElementById("cbParamVirtual1");  **  I tried using this to point to virtual field but it did not work **
    var totalField  = document.getElementsByClassName('cbFormData')[0];
    // Caspio form elements to convert local to USD cost
    var exchangerateField = document.getElementsByName("InsertRecordExchangeRate")[0];
    //var convertField  = document.getElementById("cbParamVirtual2"); **  I tried using this to point to virtual field but it did not work **
    var convertField = document.getElementsByClassName('cbFormData')[0];
      var caspioForm = document.getElementById('caspioform');
    // Event handler
    var calculateTotal = function (event)
        // TODO: Do something on value change -->
        totalField.innerHTML = unitcostField.value * unitsField.value;
        var calculateExchangeRate = function (event)
        // TODO: Do something on value change -->
        convertField.innerHTML = (1/exchangerateField.value) * unitcostField.value;
    // Run total calculation on input to any of these two fields
    unitcostField.addEventListener('input', calculateTotal);
    unitsField.addEventListener('input', calculateTotal);
    unitsField.addEventListener('input', calculateExchangeRate);
    unitcostField.addEventListener('input', calculateExchangeRate);



Thanks in Advance


Link to comment
Share on other sites

  • 1 month later...

You're using the same identifier for different calcs.

var convertField = document.getElementsByClassName('cbFormData')[0];

This is why the value from the second calc is showing up in the first element.... the first code runs perfectly, then before you can see the result the of that calc, the second code fills the data into the first element.

Might be as simple as changing to  var convertField = document.getElementsByClassName('cbFormData')[1];


Try to use ID rather than CLASS[*], you have much better control. If your Virtual Params are Cascading, that explains why you can't use ID.

Link to comment
Share on other sites

  • 2 weeks later...

I tried to use ID (only based on 1 set of formula)

var totalField  = document.getElementById("cbParamVirtual1"); 

however it does not seem to work. The Virtual Param is I have is not cascading. It is a display only field per the javascript I copied from.



Link to comment
Share on other sites

  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...