Pre-selecting choice of question based on previous value in a multi-select listbox format | XM Community
Solved

Pre-selecting choice of question based on previous value in a multi-select listbox format

  • 25 April 2021
  • 5 replies
  • 89 views

Badge +1

Hi,
Have a question where I am trying to use a simple javascript on the question like:
this.setChoiceValueByRecodeValue(1,true)
This works for me when the question is a single select.
This works for me when it is a multi-select but using list view.
It fails with no error report is it is a multi-select with listbox view. Does anyone have this working in that setup at all? Seems like a potential platform bug to me.
The list is a couple hundred options long so I can't just switch to list view to solve it.
Thanks!

icon

Best answer by faris 26 April 2021, 19:25

View original

5 replies

Userlevel 7
Badge +21

Use browser JS to make these selections. Get the container using the selector

select
.
Then set the
selected
attribute of each option that you want selected to
true
.
See this demo.

Badge +1

Hmm. This seems close. Two questions.

  1. I was planning to use the recode values for the selection, this seems to be based on the specific option being shown on the computed out, am I wrong there?

  2. This is almost too complicated, I would be selecting a single choice coming from an embedded data field, how I can I get rid of all the while's and just set the selection to the embedded data value which should match one of the recode values.

Thanks so much!

Userlevel 7
Badge +21

Can't comment on either till I see your code. The while's in my code exists to avoid duplication due to randomization.

Userlevel 2
Badge +3

Hey Brian F,
I think this is what you're looking for... I named my ED field "preSelectRecodeValue" , but you can change it to yours. I also added some checks like if there is no ED value, or if the ED value does not match a recode in the Choice List.

Qualtrics.SurveyEngine.addOnReady(function () {
let quest = this;
let qid = this.questionId;
let ed = '${e://Field/preSelectRecodeValue}';
console.log('Embedded Data value = '+ed);
if (ed.length === 0) {
console.log('No value found in ED "preSelectRecodeValue"');
return
}
let recode_to_select = ed;
console.log('Recode = '+recode_to_select)
let choiceIds_to_select = quest.getChoicesFromRecodeValue(recode_to_select);
if (choiceIds_to_select.length === 0) {
console.log('No choice was found with provided recode');
return
}
console.log(choiceIds_to_select.length.toString()+' choice(s) found with provided recode');
choiceIds_to_select.forEach((choiceId_to_select) => {
let option_to_select = quest.questionContainer.querySelector("#QR\\\\~"+qid+"\\\\~"+choiceId_to_select);
console.log('Option element to select:',option_to_select);
option_to_select.selected = true;
});
});

Badge +1

https://www.qualtrics.com/community/discussion/comment/36852#Comment_36852This was exactly what i was looking to do!!!
Thank you so much!

Leave a Reply