The showif
and skipif
tags are used to dynamically show or hide questions, tables, or text based on a condition being true. To trigger showif
, the condition can reference content on the same page, or elsewhere within the survey. Similarly, skipif
may be applied to questions and tables to indicate when to skip (or hide) a question from the respondent.
Syntax
# basic usage showif: Logical Statement skipif: Logical Statement # skipping a question or table when only a single option, row, or column would be displayed based on previous selection skip if only options: X #X = element ID skip if only rows: X skip if only columns: X
Usage note
- If a question with a
showif
tag is on a page with other content, and the other content is visible, there is no requirement to build manual validation. Assuming a globalrequired
tag, questions withshowif
will require a response if displayed, and not require a response if not displayed. - If there is no other visible content on a page aside from a widget with a
showif
tag, the page will automatically skip if theshowif
condition is not met. -
The
showif
andskipif
tags are essentially opposites. In most cases, to switch from one to the other, Survey Programmers (SPs) can use the opposite of the original condition. For example, suppose question Q2 is to be displayed to respondents if they select '1' at Q1. Although SPs could applyshowif: anyChecked($Q1,1)
to Q2, they could instead switch to usingskipif
by doing one of the following:
- use an "opposite" function – e.g., change from
showif: anyChecked($Q1,1)
toskipif: noneChecked($Q1,1)
- use the same function, but use the non-qualifying options – e.g., change from
showif: anyChecked($Q1,1)
toskipif: anyChecked($Q1,2)
- use a negation of the condition – e.g., change from
showif: $Q1==1
toskipif: $Q1 != 1
.
- use an "opposite" function – e.g., change from
- Both
showif
andskipif
can be applied to most widgets and question types, though SPs should note that these should not be applied to blocks, groups, or anew page
widget. Instead thecondition
tag should be applied to these widgets. -
In r8.0+, SPs can use non-Perl expressions with
showif
to always display or always hide questions, tables, and blocks by includingshowif: always
orshowif: never
with the widget. SPs should note that applyingshowif: always
will not override other conditions that may be applied to the variable. Similarly SPs can also useskipif: never
orskipif: always
if they prefer. See below for example. - The
skip if only options
/rows
/columns
tags can be applied to any question or table where, based on a previous selection, only a single option, row, or column would be shown to the respondent. SPs should note that the question or table these tags are applied to is only skipped, and responses are not autocoded. If need be, multiple element IDs can be specified in the tags as well, e.g.,skip if only options: 98,99
. See below for examples.
Examples
Show follow up question
In the example below, showif
is used to show Q2 if the respondent selects option 2, 'Cheese'. Q2 is hidden when the page loads and will only show once the showif
condition is true.
In the same manner, the showif
tag will work with noneChecked
, anyChecked
, allChecked
, and list functions, as well as other programming syntax (logic).
SPs can apply the skipif
tag in a similar manner as showif
, but by using the "opposite" logic. In this example, if the respondent does not select option 2 ('Cheese'), Q2A will not be shown.
On showtext
The showif
script can also be used with the showtext
widget to dynamically show other content such as text, a div, images, etc. In the following example, the showtext "SW_FAN" will be displayed only if the respondent chooses 'Definitely' or 'Sort of' in Q3.
The skipif
tag can also be applied to show text
widgets as shown at ST_FAN. The logic used here is the opposite of SW_FAN's logic, and we could use either noneChecked($Q3A,1,2)
– the "opposite" function – or anyChecked($Q3A,3,4)
– the "opposite" answers.
Auto-paging and skip logic
In r8.0+, surveys default to auto-paging (the equivalent of adding ap: y
to defaults in previous versions). As a result, Q5 and Q6 will be displayed on separate pages.
In order to have questions displayed on the same page, while still maintaining the default auto-paging behavior for the rest of the survey, add ap: n
locally to a question so it will appear on the same page as the previous one. In the below example, either Q5A or Q6A will appear on the same page as Q4A, depending on which showif
condition is met.
Tip! For information about autocoding questions that may be hidden based on conditions, see autocode.
In table rows and columns
Table rows and columns can use dynamic variable references. To show or hide a table row, place a showif
tag on that row or column definition. For example, Q7 can be on the same page as T8, or an earlier page. If the value the respondent enters in Q7 is greater than or equal to '5', the text field in the second row of T8 is displayed.
The showif
tag can also be used to dynamically show and hide table rows and columns in multi-part tables. In such cases, rows or columns may reference other variables, or other parts within the table. The ifnotset: 0
tag is included so that fields for columns that are not displayed are autocoded to '0'.
Columns in T11 are hidden depending on the answers to the previous two questions. If the respondent's answer in Q9 is greater than or equal to '5', the first column will be displayed. If the respondent answers 'Yes' to Q10, the third column will be displayed.
Internal dynamic reference for rows and columns
It is possible to reference a different column or row within the same table with a static reference, which would mimic the construction above. However, it is far more common to need a given behavior to be iterative for all table rows or columns. To do that, use __RID__
or __CID__
syntax. When used, it indicates the current row/column. Much like the [id]
reference used in the element decorator, it is iterative for all rows of the table. When acting on row 1, the value of __RID__
is '1'. For row 2, it is '2', and so on.
The following example displays the use of CID syntax. For any column where option code '3' ("Both") is selected in the first row, the second row will appear with an active box only for the selection that triggered the row's appearance. The below could also use enablewhen
instead of showif
.
Note: As with other instances of showif
, all validation is handled automatically. If a field is displayed, and all fields are required, it will be required. If it is hidden, it will not be required for the page to successfully submit.
showif always
The showif
tag can accept inputs of 'always' or 'never' in order to always display or hide a question or table from respondents. This would generally be applicable if a client decided to pull a question from their survey mid-field. They would still need to have access to the variable and the data, so switching to showif: never
hides the question, allowing the data to be kept, whereas removing the question would remove the data. If the manual switch is planned ahead of time, starting with 'always' would be an indicator for later programmers that the variable may be manually switched off.
In the example below, QPROMO could be switched to showif: never
for respondents that do not enter the survey until after the promotion has passed. For those that responded in time, their input is still valuable, and will be kept.
PROMO. What did you think of the promotion that ran this week? showif: always 1. Liked it a lot 2. Liked it a little 3. Felt neutral 4. Disliked it a little 5. Disliked it a lot
SPs should note that showif: always
does not override other conditions. If a question with showif: always
is inside a conditioned group or block, it is possible the question could still be skipped due to the other condition defining the behavior for that group or block.
Skip if only option, row, or column
The skip if only options
/rows
/columns
tags can be used to skip a question or table based on a lack of selections being piped in from a previous question. For example, if only option '99', an exclusive option, is selected in Q13, then Q14 and T15 would only have one option and one row, respectively. In that case, the client may not want to display Q14 and T15 as a result.
The skip if only
tags will then specify which option(s)/element(s), if alone, should dictate that the question or table should be skipped. Since selecting option '99' in Q13 indicates the respondent is not a peanut butter buyer, there is no need to display Q14 or T15.
Note that these tags can accept multiple IDs for when to skip the display of a question or table. For example, in this variation, selecting either options '98' or '99' would make showing Q14A or T15A to the respondent unnecessary.
Comments
0 comments
Please sign in to leave a comment.