Scripting functions for lists

Introduction

General functions

The following functions allow you to display list items for use in testing or in displays to respondents. These functions can be used in any field.

These functions return a specified list to the page. These could be used for testing, or to display items to a respondent.

Function: printListItemLabelsArray(listName: string): string

Example: {{printListItemLabelsArray ("Q1List")}}

Return: The item text of all the items in the specified list, separated by commas and spaces.

 

Function: printListItemNumbersArray(listName: string): string

Example: {{printListItemNumbersArray("Q1List")}}

Return: The item values of all the items in the specified list, separated by commas and spaces.

getList

These functions return a specified list to the page. These could be used for testing, or to display items to a respondent.

Function: getListItemLabelsArray(listName: string): string[]

Example: {{getListItemLabelsArray("Q1List")}}

Return: The item text of all the items in the specified list, separated by commas.

 

Function: getListItemNumbersArray(listName: string): string[]

Example: {{getListItemNumbersArray("Q1List")}}

Return: The item values of all the items in the specified list, separated by commas.

Dynamic list functions

The following functions are designed for dynamic list building and will only work within the context of the list being built.

When using scripts to define dynamic lists, keep the following in mind:

  • Dynamic lists must all be derived from the same source list. Some functions allow you to specify other lists for performing actions. The specified list must either be the source list of the current dynamic list or another dynamic list derived from the same source list.
  • When using custom code to construct a dynamic list, duplicate items are avoided. If an item is already in the list, it won't be added again.

To use the functions, create a dynamic list with a Custom (JavaScript) instruction.

The list manager is open to a dynamic list with an empty custom instruction.

add

Used to add items to the dynamic list from another list. When items are added, they are always added to the end of the dynamic list being created.

 

Function: add(listName: string): void

Example: add("My source list");

Result: All of the items from My source list will be added to the end of the dynamic list.

 

Function: add(listName: string, itemNumber: number): void

Example: add("My source list", 2);

Result: Item number 2 from My source list will be added to the dynamic list.

 

Function: add(listName: string, startItemNumber: number, endItemNumber: number): void

Example: add("My source list", 4, 6);

Result: Items 4, 5, and 6 from My source list will be added to the end of the dynamic list.

addAtPosition

Used to add items to a specific position in a dynamic list. If the item being added already exists in the list, that item will be moved to the position specified. If the insert position specified is greater than the number of items in the list, the item or items will be added to the end of the list.

 

Function: addAtPosition(listName: string, insertPosition: number): void

Example: addAtPosition("My source list", 3)

Result: All of the items in My source list will be added to the dynamic list beginning at position 3 in the dynamic list.

 

Function: addAtPosition(listName: string, itemNumber: number, insertPosition: number): void

Example: addAtPosition("My source list", 2, 3)

Result: Item number 2 in My source list will be added at position 3 in the dynamic list.

 

Function: addAtPosition(listName: string, startItemNumber: number, endItemNumber: number, insertPosition: number): void

Example: addAtPosition("My source list", 4, 6, 3)

Result: Items 4, 5, and 6 in My source list will be added to the dynamic list beginning at position 3 in the dynamic list.

carryForward

Used to pull items that have been selected or not selected by a respondent in a question into the dynamic list. The specified question must use the dynamic list’s source list or another dynamic list that shares the same source list as the list under construction.

 

Function: carryForward(questionName: string): void

Example: carryForward("Familiar brands");

Result: Items that were selected in the question Familiar brands will be added to the bottom of the dynamic list.      

 

Function: carryForward(questionName: string, isSelected: boolean): void

Example: carryForward("Familiar brands", false);

Result: Items that were not selected in the question Familiar brands will be added to the bottom of the dynamic list.

carryForwardCompareValues

Used to add items to a dynamic list from a specified question where the values of those items meet a comparison criterion. This function will only work for ranking and constant sum question types.

 

Function: carryForwardCompareValues(questionName: string, comparisonOperator: string, value: number): void

Example: carryForwardCompareValues("Ranking question", "<=". 3);

Result: Will add all the items from Ranking question where the value of the item is less than or equal to 3. In other words, the top three items from Ranking question will be added to the dynamic list. Valid comparison operators are < for less than, <= for less than or equal to, > for greater than, >= for greater than or equal to, = for equal to, and != for not equal to.

 

Function: carryForwardCompareValues(questionName: string, compareFn: (value: number) => boolean): void

Example: carryForwardCompareValues("Ranking question", x => x >=3 && x <=6);

Result: Applies a provided comparison function to the items in Ranking question as the parameter x and if the function returns true, the item is added to the dynamic list. In this case, it will add all items to the dynamic list that have values between 3 and 6 including 3 and 6. This example uses an arrow function syntax to define the comparison function. You can find out more about the arrow function syntax in this article.

carryForwardGridRows

Used to add items to a dynamic list from the row list in a grid question when the column selections for that row match a certain condition. This function only works for single or multi-select grids. The grid row list must be the source list of the dynamic list being built or another dynamic list that uses the same source list as the dynamic list being built.

 

Function: carryForwardGridRows(gridQuestionName: string, isSelected: boolean, columnNumber: number): void

Example: carryForwardGridRows("My grid question", true, 3);

Result: All items in the row list for the grid question named My grid question who have column 3 selected will be added to the bottom the constructed list being built.

 

Function: carryForwardGridRows(gridQuestionName: string, isSelected: boolean, columnNumberArray: number[]): void

Example: carryForwardGridRows("My grid question", false, [1,3,5]);

Result: All items in the row list of the grid question named My grid question will be added to the end of the list being constructed so long as columns 1, 3, 5 are not selected in those rows.

randomize

Used to randomize the items of the dynamic list.

 

Function: randomize()

Example: randomize();

Result: All the items that have been previously added to the dynamic list will now appear in random order.

 

Function: randomize(startPosition: number, endPosition: number): void

Example: randomize(2, 4);

Result: items 2, 3, and 4 will now appear randomly in positions 2, 3, and 4 in the new list.

remove

Used to remove items from the list being built.

 

Function: remove(listName: string): void

Example: remove("Dynamic list with shared source list");

Result: All items that are in the list named Dynamic list with shared source list will be removed from the dynamic list being built.

 

Function: remove(listName: string, itemNumber: number): void

Example: remove("Source list", 4);

Result: Item 4 from the list named Source list will be removed from the dynamic list being built.

 

Function: remove(listName: string, startItemNumber: number, endItemNumber: number): void

Example: remove("Source list", 2, 5);

Result: items 2, 3, 4, and 5 from the list named Source list will be removed from the dynamic list that is being built.

listMax

Used to set a maximum length for the dynamic list. The specified number of items will remain in the dynamic list and all other items will be removed from the dynamic list. If there are fewer items than the number specified, then nothing will happen.

 

Function: listMax(listLength: number): void

Example: listMax(4);

Result: The first four items will be kept in the list and the rest will be removed.

listMin

Used to set a minimum length for the dynamic list. Items from the source list will be added at random until there are at least as many items as specified in the dynamic list. If the number of items in the new list is greater than or equal to the number of items specified, nothing happens.

 

Function: listMin(listLength: number): void

Example: listMin(7);

Result: Items will be chosen at random from the source list and added to the bottom of the dynamic list being built until there are 7 items in the dynamic list.

setListLength

Used to specify the exact length you want the dynamic list to be. If there are fewer than the number of items specified, items are added at random from the source list until the length of the dynamic list is equal to the number specified. If there are more than the specified number, then those coming after the number of items specified will be removed.

 

Function: setListLength(listLength: number): void

Example: setListLength(4);

Result: After removing an item after item 4 or adding items at random from the source list, the dynamic list will have 4 items in it.

Adding additional logic to your script

All script for building dynamic lists execute as JavaScript. Any valid JavaScript can be executed in these scripts, making them very powerful.

If statements

If statements allow you to conditionally execute code based on a custom condition. Let's say that I have a question called Gender and I want to change the content of my dynamic list based on the gender of the respondent. For males, I want to add items 1-4 of my source list to the dynamic list, and for females, I want to add items 5-8. Both genders should see items 9 and 10, and those who choose not to specify their gender should not see the question.

In this case, let's say that the value of Gender for males is 1, for females is 2, and for those who choose not to specify, the value is 3. The name of my source list for this dynamic list is Color list.

const Gender = getValue("Gender");

if(Gender === 1){
   add("Color list", 1, 4);
} else if(Gender === 2) {
   add("Color list", 5-8);
}

if(Gender !== 3){
   add("Color list", 9,10);
}

The first thing this script does is save the value of our Gender question to a variable called Gender. Then, we see there is an if statement that uses that variable to check if the value is equal to 1. If the value of Gender for this respondent is equal to 1, the script inside the curly braces will execute, and items 1-4 will be added to the dynamic list.  

The next portion of the script is an else if statement. This condition will only be checked if the condition in the previous if statement was false. In this example, if the respondent has a value of 1 for gender, then the condition in the else if statement will not even be checked. If the respondent has a value of 2 or 3 for gender, this condition will check if the value is 2. If it is, the code inside the curly braces will be executed, and items 5-8 will be added to the dynamic list.

The final section of the code is another if statement. If the respondent has a value for Gender that is not equal to 3, meaning they selected male or female, then items 9 and 10 will be added to the dynamic list.

The result of our script for a respondent who has the value 1 for Gender will be a list that contains items 1, 2, 3, 4, 9, and 10. A respondent who has a value of 2 for Gender will have a list that contains items 5, 6, 7, 8, 9, and 10. A respondent with a value of 3 for Gender will end up with an empty list because all the conditions that get checked will return false. Because the list is empty, the question will be skipped.

For more information on JavaScript if statements, this tutorial may be useful.

Debugging your list building script

Whenever you use custom code to construct a dynamic list, you should thoroughly test your code to ensure that it’s doing what you intended in every use case. Any errors produced by your code will prevent the list building script from continuing as soon as the error occurs. All errors that are thrown in your code will be logged to the console.
 
In most browsers, you can open the console by finding the browser’s menu, opening the tools submenu, and selecting Developer tools. Most browsers also support using the F12 key to open the developer tools. You can also find instructions for your specific browser and version with a quick search, for example, “how to open the developer tools in Chrome.”
 
Lets say we had the following script that adds items from the source list one at a time.

add("Source list", 1);
add("Source list", 2);
throw("My list building script threw and error");
add("Source list", 3);
add("Source list", 4); 

When we test the survey, we will see that the question using this dynamic list only has 2 items in it, whereas it was intended to have 4 items. If you open the console in the browser, you will see the following:

Console Error

Note the error message says, “My list building script threw an error,” just like the error being thrown in our script. The first two items were added successfully, but then an error was thrown, stopping the execution of the script before the third and fourth items could be added. As a result, we end up with a list that only has 2 items. Errors in your dynamic list scripts will not prevent the user from continuing in the survey.
 
You can find more helpful information and resources about debugging your scripts in our Custom JavaScript help article.