Reorder answers, columns, and rows using scripting

Use scripting to reorder child elements in questions.

You can reorder child elements in the following question types:

  • Single Choice
  • Multiple Choice
  • Single Choice Grid
  • Multiple Choice Grid
  • Allocation
  • Rank Order
Table 1. Choice, Allocation, and Rank Order questions
Function or property Description
const q1= page.getElement('Q1');
q1.reorderElements([authoredIndex1, ...]) returns [authoredIndex1, ...]

Sets the order of child elements based on the authored indexes (zero-based), and returns an array with all the elements' authored indexes.

If the authored index is not present in the array because of masking or any other reason, it will be ignored.

If the number of authored indexes provided is less than the number of elements in the array, the first matches will be ordered first.

Table 2. Grid questions
Function or property Description
const grid = response.getDataPoint('Grid');
grid.elements[0].reorderElements([...])
Reorders answers (zero-based). grid.elements[0] references the answers.
grid.elements[1].reorderElements([...]) Reorders statements (zero-based). grid.elements[1] references the statements.
Note: You cannot use grid.reorderElements([...]) to reorder the entire grid. You can only use grid.elements[0] or grid.elements[1] to reorder answers or statements, respectively.
Example

A Single Choice question has 5 answer options. 2 answers are masked. The zero-based authored indexes range from 0 to 4.

Single Choice question

a - 0

b - 1 (Masked)

c - 2

d - 3 (Masked)

e - 4

Function example Description
q1.reorderElements([]) => [0,2,4] Passing an empty array returns the current order of elements.
q1.reorderElements([1,0,3,4]) => [0,4,2] This function example reorders the answers as b, a, d, and e. Because b and d are masked, a and e are returned. 2 was not provided, so answer c appears at the end of the array.
q1.reorderElements([2]) => [2,0,4] 2 (answer c) is set at the beginning of the array. The remaining non-masked answers a and e appear at the end of the array.