JavaScript for Rank Order question limited to 3 option (columns) | XM Community
Skip to main content

I need a piece of custom code to get a rank order question with radio buttons working. The question needs to show only 3 columns (#1, #2, #3) for a list with 3+ items, so the respondent can select his/her top 3 from that list.
I have a piece of code that appears to work, but Qualtrics somehow faults at a certain moment and puts all the items into a single answer option.
I'm in dire need, this survey needs to go live soon. Willing to pay for a solution that works, for that matter.
Below the code and a series of screen dumps showing the problem.
Qualtrics.SurveyEngine.addOnload(function()
{ /*Place your JavaScript here to run when the page loads*/
for(var i = 3; i < 100; i++){
jQuery("#"+this.questionId+" tr.Answers th:eq("+i+")").hide();
   jQuery("#"+this.questionId+" .ChoiceStructure tbody tr").find('td:eq('+i+')').hide(); }
});
This is how it shows in the editor
image.pngThis is how it should show in the survey (preview)
image.pngBut below is how Qualtrics transforms the question and subsequently how it is presented. Note: it does so with most questions, but not all. And often after a while ...
image.png
In the preview it looks like this (one column only...)
image.png

This should work for you!
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
for(var col=3;col<=26;col++){

jQuery(".ChoiceStructure .Answers").find("th:eq("+col+")").css("visibility","collapse");

jQuery(".ChoiceStructure tbody tr").each(function(){

jQuery(this).find("td:eq("+col+")").css("visibility","collapse");

})
}
});

This way, it is just collapsing the columns instead of hiding/removing them completely.


Tnx Jacqueline, it seems to work indeed. However ... the column with item labels is cut off, creating a bit awkward presentation of the list. But I guess this can be fixed easily.
image.pngAnother tweak that seems to work for now (don't ask why, but trial and error led me to this): changing the Validation Type to "Nono" and back to "Must rank between" again. It appears the system is stable now, also with the 'old' JavaScript.
I'm keeping my fingers crossed ... Qualtrics support is looking into the matter, as this kind of error should not happen (or at least we should know what causes the automatic transformation in some case, but not all). I have multiple questions with the same script and sometimes it changes, sometimes it stays as supposed to.


You must have been done with this by now, but here's a solution that works:
Demo here.
Qualtrics.SurveyEngine.addOnReady(function(){
rows = this.getChoiceContainer().querySelectorAll("tr");
for(i=0;i bl = rows[i].children;
for(j=4;j bl[j].hide();
}
}
});



Leave a Reply