Solved
Create weights in JavaScript based on ranking question
I need to create weights in-survey for rank-order in drag/drop ranking questions. Example, rate these car brands on safety: BMW, Volvo, Ford, Kia,BToyota The #1 ranked car gets a weight of 1, 2nd ranked a weight of .75, 3rd ranked a weight of .5, 4th a weight of .25, 5th a weight of 0. These weights will be stored in embedded variables like BMW_Safety_Weight, Volvo_Safety Weight, etc. How can I do this using javascript? Thanks!
Extra credit: We have an embedded variable called BrandCount (between 2 and 10 brands) The above example is a 5 point scale, for 5 brands. If 4 brands were to be ranked, the weights would be 1, .67, .33, 0. Ideally we'd like to apply the weighting scale that corresponds to the BrandCount, but I can figure that out later. As always, your help is greatly appreciated!
Best answer by Anonymous
Hello @Tom_R ,
Assuming you are using "Rank order" -> "Drag and Drop" question type.
Step 1: Create five embedded data(BMW_Safety_Weight, Volvo_Safety_Weight, Ford_Safety_Weight, Kia_Safety_Weight, BToyota_Safety_Weight) in the survey flow before the rank order question. Now, please note the vehicle name in the embedded data must be exactly same with that provided in the question's option.(This is done to make js code smaller)
Step 2: Paste the following code in the `js(onReady)` of the rank order of the question.
jQuery(document).on('DOMSubtreeModified', function(){
var aa=[];
jQuery("ul.ui-sortable li.ui-sortable-handle > span.label").each(function(){
aa.push(jQuery(this).text().trim());
Qualtrics.SurveyEngine.setEmbeddedData( aa[0]+'_Safety_Weight', '1' );
Qualtrics.SurveyEngine.setEmbeddedData( aa[1]+'_Safety_Weight', '0.75' );
Qualtrics.SurveyEngine.setEmbeddedData( aa[2]+'_Safety_Weight', '0.5' );
Qualtrics.SurveyEngine.setEmbeddedData( aa[3]+'_Safety_Weight', '0.25' );
Qualtrics.SurveyEngine.setEmbeddedData( aa[4]+'_Safety_Weight', '0' );
});
});
Step 3: Now use those five embedded data(BMW_Safety_Weight, Volvo_Safety_Weight, Ford_Safety_Weight, Kia_Safety_Weight, BToyota_Safety_Weight) wherever required
View originalLeave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.