Solved
How to dynamically show Next button based on a certain number of selections in multi choice question
Hi,
I am dynamically showing statements in a multi-choice question based on the selections of previous questions. At this question, if the number of statements displayed is less than or equal to 5 then they get auto coded and the Next button is auto-submitted. And if the number of statements displayed is more than 5 than the respondent should be able to proceed only after selecting 5 statements. So, I am trying to hide the Next button on Page load and when the number of selection is equal to 5 then the Next button should be displayed. I was able to code the first part when the number of selections in less than or equal to 5 but having issues with the second part and not able to dynamically show the Next button on 5 selections. Below is my code...It would be great if someone could help me solve this issue.
Qualtrics.SurveyEngine.addOnload(function()
{
var qobj1 = this;
this.hideNextButton();
});
Qualtrics.SurveyEngine.addOnReady(function() {
var qobj = this;
var displayedChoices = [];
var selectedChoices = [];
jQuery.each(qobj.getChoices(), function(index, value) {
if(qobj.getChoiceDisplayed(value)) displayedChoices.push(value);
});
jQuery.each(qobj.getChoices(), function(index, value) {
if(qobj.getSelectedChoices(value)) selectedChoices.push(value);
});
if(displayedChoices.length <= 5) {
qobj.setChoiceValue(displayedChoices[0], true);
qobj.setChoiceValue(displayedChoices[1], true);
qobj.setChoiceValue(displayedChoices[2], true);
qobj.setChoiceValue(displayedChoices[3], true);
qobj.setChoiceValue(displayedChoices[4], true);
qobj.showNextButton();
qobj.clickNextButton();
}
if(displayedChoices.length > 5 && selectedChoices.length!=5){
alert("Please select 5 statements");
}
else
qobj.showNextButton();
});
Best answer by TomG
The code I provided was meant to replace ALL your code. So it would be like:
```
Qualtrics.SurveyEngine.addOnReady(function() {
var qobj = this;
qobj.hideNextButton();
var cbs = jQuery("#"+qobj.questionId+" input[type=checkbox]");
if(cbs.length <= 5) {
cbs.prop("checked",true);
qobj.clickNextButton();
}
else {
var numSelected = cbs.filter(":checked").length;
if(numSelected >= 5) qobj.showNextButton();
cbs.click(function() {
numSelected = cbs.filter(":checked").length;
if(numSelected >= 5) qobj.showNextButton();
});
}
});
```
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
