Jump to content

how to reference Calculated Virtual Field Value in Javascript

Recommended Posts

Please bear with me as i try to unfold this.

I have a submission datapage with a Virtual Field  (Virtual23) where as a Calculated Field i'm using a SELECT statement to fetch what I need correctly.  This is successful.

Where i'm struggling is how to reference this value in javascript.   I'm trying to store the value in a varable ...

I've tried this: 

var  x = document.getElementById("cbParamVirtual23").value;

and it doesn't seem to work.  I'm assuming the value fetched by the SELECT statement should return a string.

I've tried the below code to test if it works, but it fails. 

document.getElementById("ElderbergHTMLBlock1").innerHTML = x;
document.getElementById("ElderbergHTMLBlock1").innerHTML = x.toString();


Link to comment
Share on other sites

Solution Involves 2 Parts:

Part 1:  You need to use setTimeout() method to delay the code such that it runs AFTER the calculated fields get computed.  See https://www.w3schools.com/jsref/met_win_settimeout.asp 


Part 2:  You need to reference calculated fields using the document.getElementsByName with index 0  instead of by ID, this is because caspio adds some alphanumeric codes after the Virtual Parameters ID when its set to a Calculated Field.

See Code:
var x = document.getElementsByName("cbParamVirtual27")[0].value;
document.getElementById("ElderbergHTMLBlock1").innerHTML = x;

Link to comment
Share on other sites

  • 2 years later...

Hi! Another solution that I implemented to reference the value of a calculated value is by using a calculated field instead. Then, I added a new virtual field and set it as hidden to receive the value of calculated field 1.


After that, I used the ID of Virtual 1 to get the value of my calculated field.

var calc = document.getElementById("cbParamVirtual1").value;

I know this is not the best way but it does the job in getting the value of calculated field.

Link to comment
Share on other sites

On 7/15/2022 at 7:29 PM, Meekeee said:

Hi @AveEd - In the Calculated Field, you can use the Field Picker and select the Virtual Field. For more information, you can check it here: https://howto.caspio.com/datapages/reports/advanced-reporting/calculations-in-forms-and-reports/

Can you provide a screenshot of the "field picker" where we can get virtual values to be used in calculated please?

Link to comment
Share on other sites

  • 9 months 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...