Copying a multiple choice selected option text to embedded data for multiple different questions. | XM Community
Solved

Copying a multiple choice selected option text to embedded data for multiple different questions.

  • 16 February 2019
  • 2 replies
  • 16 views

Badge +6
Hello!

I have the following code for copying selected multiple choice text to an embedded data field "choice1":

`Qualtrics.SurveyEngine.addOnload(function()
{

var currentQuestionID = this.getQuestionInfo().QuestionID
console.log("Current Question ID is: " + currentQuestionID)

var resultEmbeddedName = "result_" + currentQuestionID.substring(3) //e.g. result_6

$('NextButton').onclick = function (event) {


var questionObject = Qualtrics.SurveyEngine.getInstance(currentQuestionID)
var currentResponse = questionObject.getSelectedChoices()[0] //in case more than one is selected, it will only work here to take one!
var theQuestionInfo=questionObject.getQuestionInfo()
var choicesObject=theQuestionInfo.Choices
var thisChoiceObject=choicesObject[currentResponse]
var currentChoiceText=thisChoiceObject.Text

console.log("Number of the current choice is " + currentResponse)
console.log("Text of the current choice is " + currentChoiceText)

Qualtrics.SurveyEngine.setEmbeddedData('choice1', currentChoiceText)

Qualtrics.SurveyEngine.navClick(event, 'NextButton')
}

});`

Basically, this code works once, on the first question displayed, but then when I try to copy future responses to fields "choice2", "choice3" etc. it stops working. Does anybody know why this could would only work once in a survey?

Thank you!

Josh
icon

Best answer by TomG 18 February 2019, 15:40

View original

2 replies

Userlevel 7
Badge +27
I'm surprised it worked at all because of the click handler on the Next button. Use addOnPageSubmit instead. Then delete the navClick line at the end.

There are several things you are doing the hard way. For example, at the start of your script you could do this:
```
var currentQuestionID = this.questionId;
var questionObject = this;
```
Badge +6
Very appreciated Tom. This worked.

Leave a Reply