kytan5 Posted November 22, 2016 Report Share Posted November 22, 2016 Hi 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. Eg 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); </script> Thanks in Advance Quote Link to comment Share on other sites More sharing options...
LWSChad Posted December 22, 2016 Report Share Posted December 22, 2016 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. Quote Link to comment Share on other sites More sharing options...
kytan5 Posted January 2, 2017 Author Report Share Posted January 2, 2017 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. Quote Link to comment Share on other sites More sharing options...
Kurumi Posted June 24, 2019 Report Share Posted June 24, 2019 For calculations in DataPage. You might want to check this post for new updates: I hope it helps! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.