The dynamicvalue
function sets a question's value based on another question or variable. It can be used for tasks like automatically coding closed-end values or performing respondent-facing calculations.
Details
-
dynamicvalue
can be a standalone widget or an inline set of tags within another widget, such as on a question, a row, or a column. - The controlling questions or variables, as well as the target question, can be any question type.
- All question references can be located on the same survey page, different survey pages, or a combination of both.
- When using
dynamicvalue
with a checkbox question, note that any existing data will be cleared, and only the specified value will remain. -
Tables using
dynamicfilter
accept__RID__
/__CID__
syntax to target a specific row/column or to iterate through all rows/columns. - Similarly, tables using
dynamicvalue
can accept__ALLROWS__
and__ALLCOLS__
syntax to target all table rows or columns.
Tip! If you need to set a simple value to a question or variable when the page loads, try the dvalue
tag.
Tags
dynamicvalue
can be a standalone widget or an inline tag within a question or variable, with slight variations in supporting tags depending on the method. For example, if dynamicvalue
is used as a standalone widget, the condition
tag is used on a separate line. However, if dynamicvalue
is attached to a question, the dynamiccondition
tag is used. Adding "dynamic" to the tag name gives the necessary instructions for the software to apply the condition correctly.
As a widget | As a tag | Description |
condition |
dynamiccondition |
An expression (conditional statement); if the expression evaluates to "true," the value is applied to the question or table. |
filter |
dynamicfilter |
Only applicable for tables. This tag targets specific rows or columns, excluding autoothers, using |
lock |
dynamiclock |
This tag prevents the respondent from changing the set values. Both |
question |
-- | The question ID where the value will be applied. |
table |
-- |
The table ID where the value will be applied. |
value |
dynamicvalue |
The value being set to the question or table widget. The When used as an independent widget, |
Additional examples
Setting a value from a multiselect question
The value
tag can accept arrays when coding (setting a value to) multi-select questions. Reminder, when using dynamicvalue
with a checkbox question, note that any existing data will be cleared, and only the specified value will remain.
To apply the condition
tag, write the conditional expression without the leading "if." Here, if option 1, 2, or 3 is selected at Q3, then the answer from Q3 plus option 4 will be set to Q4.
Using the 'lock' tag
The lock
tag prevents respondents from modifying the target question, which is "locked" by default. In the example below, the target question is the total row, 'Q9RALL'. With lock: n
applied, the respondent can alter the total row even after it is calculated.
The above summing row example can be written to dynamically reference all table rows with __ALLROWS__
reference if it is written inline.
Calculating row and column totals and the 'dynamicfilter' tag
In this first example, __ALLCOLS__
sums across all columns for each row and applies the total sum to the column specified in the dynamicfilter
tag. The filter iterates through each column using the __CID__
placeholder until it finds the column that evaluates to 'true', which is column 3. The total sum is then applied to column 3, the 'TOTAL' column.
Then, similar to the previous example, here __ALLROWS__
sums across the rows and applies the total sum to the row specified in the dynamicfilter
tag. The filter iterates through each row using the __RID__
placeholder until it finds the row that evaluates to 'true', which is row 3. The total sum is then applied to row 3, the 'Total Spend' row across the bottom.
Calculating annual spend based on monthly average
The following example uses dynamicvalue
to take the respondent's input on their average spend on various products and then extrapolate their approximate annual spend on these products.
Incorporating the 'toFixed' function
dynamicvalue
mainly uses JavaScript for its calculation, some undocumented functions may help experienced users. Try using .toFixed()
to limit the decimal precision of the resulting value. For example, to limit a respondent's input to two decimal places, use the following .toFixed(2)
.
Autofilling a single row in sum to 100 table
In the following example, the table includes a row that sums to 100%, with the "In person" row autofilled to help prevent errors—sometimes called a "plug row."
The dynamicfilter
targets row 3 ("In person"), and the dynamicvalue
calculates its value by subtracting the totals of row 1 and row 2 from 100, ensuring the remaining percentage is allocated to row 3. This row is "locked" and cannot be modified by the respondent.
Additionally, the HTML below the table includes JavaScript that makes the 'SCT' row read-only.
Comments
0 comments
Please sign in to leave a comment.