Hi Community,
I have a matrix question with randomized choices on a 5-point scale of Extremely familiar to Extremely unfamiliar. In the next question, also a matrix, I need to display the choices that the respondent is familiar with and keep the same randomization order. I tried to carry forward the choices then add display logic to each choice, but I can't add display logic to carry forward choices. I changed from carry forward choices to adding all choices then applying display logic, but now I don't have the same randomization.
Is there a way with JavaScript or some other method to get that randomization order the same?
Page 1 / 1
Yes, there are two ways:
1. Add a script to the first question that saves the randomization order to an embedded data variable. Use display logic (not carry forward) on the follow-up question and add a script to reorder the rows using the embedded data variable from the first question.
2. Use carry forward on the second question. Add a script where you pipe the selected answers from the first question into the script then hide the rows that don't apply. You can't use force response on this question.
Because of the force response limitation, the first method is preferable.
1. Add a script to the first question that saves the randomization order to an embedded data variable. Use display logic (not carry forward) on the follow-up question and add a script to reorder the rows using the embedded data variable from the first question.
2. Use carry forward on the second question. Add a script where you pipe the selected answers from the first question into the script then hide the rows that don't apply. You can't use force response on this question.
Because of the force response limitation, the first method is preferable.
Thanks. I've successfully saved the choice order using
`Qualtrics.SurveyEngine.setEmbeddedData('Q15_Order',this.question.runtime.ChoiceOrder.toString());`
But I can't find a way to set the order with the JavaScript API. Do you know of a way with the API or do I need to change the order of the tables rows in the DOM?
`Qualtrics.SurveyEngine.setEmbeddedData('Q15_Order',this.question.runtime.ChoiceOrder.toString());`
But I can't find a way to set the order with the JavaScript API. Do you know of a way with the API or do I need to change the order of the tables rows in the DOM?
> @HMRS said:
> Thanks. I've successfully saved the choice order using
>
> `Qualtrics.SurveyEngine.setEmbeddedData('Q15_Order',this.question.runtime.ChoiceOrder.toString());`
>
> But I can't find a way to set the order with the JavaScript API. Do you know of a way with the API or do I need to change the order of the tables rows in the DOM?
You need to change the order of the tables rows in the DOM.
> Thanks. I've successfully saved the choice order using
>
> `Qualtrics.SurveyEngine.setEmbeddedData('Q15_Order',this.question.runtime.ChoiceOrder.toString());`
>
> But I can't find a way to set the order with the JavaScript API. Do you know of a way with the API or do I need to change the order of the tables rows in the DOM?
You need to change the order of the tables rows in the DOM.
In case this helps anyone in the future here is the code I used to reorder the table rows. This will only work for matrix questions if only one question is on the page.
If anyone has any suggestions for improvements, or see any potential problems let me know.
`//Get choices order from embedded data`
`var Q15OrderStr='${e://Field/Q15_Order}';`
`//Split the scring into an array`
`var Q15OrderArr=Q15OrderStr.split(",");`
`//Get choice order for question`
`var choicesArr=this.question.runtime.ChoiceOrder;`
`//Remove any choices from Q15OrderArr that are not in choicesArr`
`var choiceOrderArr=Q15OrderArr.filter(it => choicesArr.includes(it));`
`//Get all the choice rows`
`var tableRows=document.querySelectorAll(".ChoiceRow");`
`//Save the parent node`
`var p=tableRows[0].parentNode;`
`//Remove rows`
`for (var i = 0, l = tableRows.length; i < l; i++) {`
` tableRows[i].parentNode.removeChild(tableRows[i]);`
`}`
`//Add rows back in order`
`for(i=0,l=choicesArr.length;i<l;i++){`
` p.appendChild(tableRows[choicesArr.indexOf(choiceOrderArr[i])]);`
`}`
If anyone has any suggestions for improvements, or see any potential problems let me know.
`//Get choices order from embedded data`
`var Q15OrderStr='${e://Field/Q15_Order}';`
`//Split the scring into an array`
`var Q15OrderArr=Q15OrderStr.split(",");`
`//Get choice order for question`
`var choicesArr=this.question.runtime.ChoiceOrder;`
`//Remove any choices from Q15OrderArr that are not in choicesArr`
`var choiceOrderArr=Q15OrderArr.filter(it => choicesArr.includes(it));`
`//Get all the choice rows`
`var tableRows=document.querySelectorAll(".ChoiceRow");`
`//Save the parent node`
`var p=tableRows[0].parentNode;`
`//Remove rows`
`for (var i = 0, l = tableRows.length; i < l; i++) {`
` tableRows[i].parentNode.removeChild(tableRows[i]);`
`}`
`//Add rows back in order`
`for(i=0,l=choicesArr.length;i<l;i++){`
` p.appendChild(tableRows[choicesArr.indexOf(choiceOrderArr[i])]);`
`}`
deleted
Using jQuery would have made it easier. You don't need to remove the rows. Appending them to the parent will move them into the correct order. Making the selector specific to the question will allow you to have more than one question on a page (i.e., "#"+this.questionId+" .ChoiceRow")
Thanks Tom. Good suggestions.
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.