Default choices for single and multiple reponses | XM Community
Skip to main content

Hello,
I am trying to tinker with a jquery in order to display a default value based on embedded data. I have tried quite a few codes found here but none of them work.
The last one I tested for a single response :
var select = jQuery("#"+this.questionId+" select");>
if("${e://Field/HDP01}" == "Oui") select.val("Oui");
if("${e://Field/HDP01}" == "Non") select.val("Non");
Can you help me ?

Try the below code:
var value = 'Oui';
if("${e://Field/HDP01}" == "Oui"){
jQuery('#'+this.questionId+' select option:contains(' + value + ')').each(function(){
if (jQuery(this).text() == value) {
jQuery(this).attr('selected', 'selected');
return false;
}
return true;
});
}


thanks but it still doesn't seem to work :(


https://www.qualtrics.com/community/discussion/comment/27036#Comment_27036The code is actually working. Hope you have removed you previous code. Also, the above code is just for one case, try duplicating it as shown below. Also, make sure the var value = ......value is exactly same as that you have out in the option text, and check same for embedded data.

var value = 'Oui';
if("${e://Field/HDP01}" == "Oui"){
jQuery('#'+this.questionId+' select option:contains(' + value + ')').each(function(){
if (jQuery(this).text() == value) {
jQuery(this).attr('selected', 'selected');
return false;
}
return true;
});
}

value = 'Non';
if("${e://Field/HDP01}" == "Non"){
jQuery('#'+this.questionId+' select option:contains(' + value + ')').each(function(){
if (jQuery(this).text() == value) {
jQuery(this).attr('selected', 'selected');
return false;
}
return true;
});
}


you're really kind to help me as much unfortunately I can't seem to have a choice selected by default. I tried to recode my variable in 1 or 2 to see if it would work better without "" but no.


Keolis - The above code, shared by rondev is working fine but since this is not working for you, so I have little bit different code, you can use it and you don't have to write the different if conditions too.
var em1 = "${e://Field/HDP01}"
jQuery('#'+this.questionId).find('select option').each(function(){
if(jQuery(this).text() == em1)
{
jQuery(this).prop('selected',true)
}
});


thanks SurajK, after chatting with rondev it seems like in my first code I was not using the options for my single choice question, not drop down?


Keolis - If you want to select the options in single choice question based on your embedded data then you can use like,
if("${e://Field/HDP01}" == "Oui") {this.setChoiceValue(1,true)} // If Oui is your first choice
if("${e://Field/HDP01}" == "Non") {this.setChoiceValue(2,true)} // If Non is your second choice
and so on..


thank you that was exactly what i was looking for.
two other questions at once:

  • how to adapt this script for a multiple choice question with modalities separated by "," ?

  • in the end, I'll use these two scripts in a 3 column matrix. how should i proceed to identify each of the columns in the jquery?


For multiple choice question, you meant to say based on the multiple answer recode then you will have to split by comma and then you will have to run a for loop to check each answer which is separated by comma, example
var multi = "${q://QID1/SelectedChoicesRecode}"
var arr1= multi.split(',')
for(var i=0;i{
if( arr1[i] == "Oui") {this.setChoiceValue(1,true)}
}
For matrix, you can use the code like,
this.setChoiceValueByRecodeValue(1,1,true);
where,
First 1 - First choice
Second 1 - First column


great i made the loop work :)
however the matrix code doesn't seem to work for me...
jQuery("#"+this.questionId+" .InputText").flatpickr({minuteIncrement: 1, enableTime: true, noCalendar: true, allowinput: true, dateFormat: "H:i",time_24hr: true});
if("${e://Field/HDP01}" == "Oui") {this.setChoiceValue(1,2,true)} // If Oui is your first choice
if("${e://Field/HDP01}" == "Non") {this.setChoiceValue(2,2,true)} // If Non is your second choice


Capture.PNG


is there anybody who could help me ?
I feel that I am not far from finding an answer :)


https://www.qualtrics.com/community/discussion/comment/27103#Comment_27103Okay So just to explain you, below are the code that you can use:
For first row Oui radio button selection use below code:
jQuery(".Choice:eq(0) .c7 input").prop("checked",true);
For first row Non radio button selection use below code:
jQuery(".Choice:eq(0) .c8 input").prop("checked",true);
For first row Lundi checkbox button selection use below code:
jQuery(".Choice:eq(0) .c11 input").prop("checked",true);
For first row Mardi checkbox button selection use below code:
jQuery(".Choice:eq(0) .c12 input").prop("checked",true);
For first row Mercredi checkbox button selection use below code:
jQuery(".Choice:eq(0) .c13 input").prop("checked",true);
For first row Jeudi checkbox button selection use below code:
jQuery(".Choice:eq(0) .c14 input").prop("checked",true);
For first row Vendredi checkbox button selection use below code:
jQuery(".Choice:eq(0) .c15 input").prop("checked",true);

Similarly, in the above code if we change eq(0) to eq(1) then we get second row selection and so on.


Leave a Reply