Solved
Adding Answer Choices to Multi Select Box using JavaScript
Hello!
In my survey, I ask respondents about a list of names and will use these names as answer choices in following questions. I'm currently trying to add additional answer choices to a multi-select box. I managed to have "Simba" show up as an additional choice in the select box by adding the following to the addOnReady:
```
var newOption = "Simba";
var option = document.createElement("option");
option.text = newOption;
jQuery("#"+this.questionId+" select").append(option);
```
However, if I select "Simba", this is not recorded by Qualtrics. I saw that the other option elements have additional attributes, so i tried adding those to my new option (the Question is QID7).
```
option.id= "QR~QID7~i00";
option.setAttribute("class", "Selection");
option.setAttribute("role", "option");
option.setAttribute("aria-selected", "false");
option.value = "QR~QID7~i00";
option.setAttribute("data-runtime-text", "Runtime Text test00");
```
The problem remains though. Also, the attribute "aria-selected" does not change when I select Simba, but it does change with the other, regular answer choices. I tried solving this by adding
```
if(jQuery(option).prop( "selected" ))
{
option.setAttribute("aria-selected", "true");
}
```
But again, this did not help.
Any help would be appreciated!
Best answer by Anonymous
Paste the below code in the js(OnReady) of the DD question:
var that = this.questionId;
jQuery("#"+that+" select").on('change',function(){
Qualtrics.SurveyEngine.setEmbeddedData("ED_DD_Ans", jQuery("#"+that+" select").find("option:selected").text());
});
Make sure you have embedded data "ED_DD_Ans" declared/created in the survey flow before this question.
View originalLeave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.