Conditional count of checked multiple choice answers | XM Community
Question

Conditional count of checked multiple choice answers

  • 22 June 2021
  • 6 replies
  • 216 views

Userlevel 5
Badge +11

Hi All,

My starting point is this thread: https://community.qualtrics.com/discussion/1793/creating-counter-showing-number-of-selected-choices-that-updates-dynamically
there is a test QSF courtsey of MohammedAli_Rajapkar (thanks!)
I'd like to split and cout the selections (clicked) multiple choices into three groups, Group1, Group2, Group3
image.png
I'm more interested in updating an embedded field than displaying the count in the question but for now will stick with this route as then at least I know it will work and then I can use the setEmbededData function. My problem is how to setup a conditional count in JavaScript

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/


var QID = this.questionId ;
jQuery("#" + QID + " input[type='checkbox']").on("click", function() {

   
  var number = 0;
var Group1 = 0;
var Group2 = 0;
var Group3 = 0;

jQuery("#" + QID + " input[type='checkbox']:checked").each(function(){
//if the clicked item is QR~QID1~1 or QR~QID1~2 or QR~QID1~3 then Group1=Group1 + 1;
//if the clicked item is QR~QID1~4 or QR~QID1~5 or QR~QID1~6 then Group2=Group2 + 1;
//if the clicked item is QR~QID1~7 or QR~QID1~8 or QR~QID1~9 or QR~QID1~10 then Group3=Group3 + 1;
 number = number + 1
});

  jQuery('#chkcount').text(number);
  jQuery('#chkcount').text(Group1);
  jQuery('#chkcount').text(Group2);
  jQuery('#chkcount').text(Group3);

});

I am sure there is a much more elegant way of doing this but hopefully this is a good starting point.
Please could I get a little bit of help as to how when cycling through each checked item I know which QR~QID~x it's on.

Thanks

Rod Pestell


6 replies

Userlevel 5
Badge +11

Hi All,

I got a little further on this but I think maybe it's going to be more complicated than I thought.
This code identifies what I thought was the ChoiceID of the button, but the problem is if you insert or change the order of the buttons it doesn't work - it reports the position of the button
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/


var QID = this.questionId ;
jQuery("#" + QID + " input[type='checkbox']").on("click", function() {

   
  var number = 0;
  var element;

jQuery("#" + QID + " input[type='checkbox']:checked").each(function(){
number=number + 1;

var choiceNum = jQuery(this).attr("choiceid")
console.log(choiceNum)
});

  jQuery('#chkcount').text(number);

});


});

I thought that it would return the ID of the Answer / button that is clicked but it turns out that it's the position / ID that is returned. For example when you move button 10 (I've called it CH10) to the top...

image.pngand select the buttons
image.pngin the console log you see this in the console log
1
2
if you swap the buttons back (so CH10 goes back to the bottom) and click CH1 and CH2
you still get in teh console window
1
2

Yet in the following page you can pick up the recode (you have to have recode selected) you do get at least the correct recodes and labels.
image.png
is this as expected or is my
jQuery(this).attr("choiceid")
syntax not correct?
My objective is to ensure I know which button is being pressed and so the ID I use needs to follow the buttons around incase they are reordered or a new one is added. If I need to work with the recode ie ${q://QID1/SelectedChoicesRecode} I presume I'll need to move the JS code to a page submit function the following question but then I'm not too sure how I go about parsing the comma delimited recode result.

Any thoughts on this would be most welcome.

Thanks

Rod Pestell

Userlevel 5
Badge +11

ah! slight problem with my 'add the JS code on the following question' I don't have a question that I can fit onto. Is there anyway to return the SelectedChoicesRecode or similar using the addOnPageSubmit or addOnUnload?

Userlevel 5
Badge +11

I've made some progress but have a few questions / problems. I managed to create a question, hide it via JS but still allows the code to run behind the scenes and auto advance it on. That means I can use ${q://QID1/SelectedChoicesRecode}. :)
The next step was to split the string into an array, loop through it and use the if/else or switch/case method to do a group count up of the answers selected on the previous question. I thought if/else would get rather confusing with all the nesting and so decided to give switch/case else. In VBA it's really easy to use and you can do multiple conditioning / logic operators (eg. x>0 and x<5). However I couldn't get it to work here. I think there were other issues in that the string from SelectedChoicesRecode or the split function was putting a space for single digits at the front, ie '10' came out as '10' but 1 came out of the split function as ' 1' - really odd! So I had to use parseInt() and Number(). Perhaps that was overkill but I'm working blind on this as the only method I have to debug this is via the Chrome console. :(

The next problem I found was that I came across on many sites the fact that you could use && in the switch/case but for some reason it wasn't listening to it and would only function on the first condition. Can anyone advise as to why?

In the end I split the case to each individual number. Not clever but better than trying to work with nesting of if/else.... unless you think otherwise? Here's my final code. Excuse all the console.log( parts! But that was the only way I cold try to find out why it wasn't working properly.

Would really appreciate some guidance to try and tidy this code up to make it more clever.

Thanks

Rod Pestell
ps, discoverd that if you don't have a normal question after this one and you do a preview block which then takes you to an End of Survey message, it throws an error. So always have an additional question after to allow all the javascript to work whilst testing.
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/


jQuery(".JFE").hide()

var QID1_Recode = ("${q://QID1/SelectedChoicesRecode}");
var QID1_Recode_Arr = QID1_Recode.split(',')
var QID1_Recode_Arr_Parsed

var Group1 = 0;
var Group2 = 0;
var Group3 = 0;

for (var i = 0; i < QID1_Recode_Arr.length; i++){
QID1_Recode_Arr_Parsed = Number(parseInt(QID1_Recode_Arr[i]))
console.log("Parsed = '" + QID1_Recode_Arr_Parsed + "'");
/*you have to parse as sometime get a space occuring*/

//this switch method doesn't work due to the && sign I think
/*switch (true){
case QID1_Recode_Arr_Parsed > 0 && QID1_Recode_Arr_Parsed < 4:
Group1 = Group1 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
case QID1_Recode_Arr_Parsed > 3 && QID1_Recode_Arr_Parsed < 7:
Group1 = Group1 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
case QID1_Recode_Arr_Parsed > 6 && QID1_Recode_Arr_Parsed < 11:
Group1 = Group1 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
default:
console.log("didn't match any case")
}*/

switch (QID1_Recode_Arr_Parsed){
case 1:
Group1 = Group1 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
case 2:
Group1 = Group1 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
case 3:
Group1 = Group1 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
case 4:
Group2 = Group2 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
case 5:
Group2 = Group2 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
case 6:
Group2 = Group2 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
case 7:
Group3 = Group3 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
case 8:
Group3 = Group3 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
case 9:
Group3 = Group3 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
case 10:
Group3 = Group3 + 1;
console.log("i= " + QID1_Recode_Arr_Parsed);
break;
default:
console.log("didn't match any case")
}
}
console.log(QID1_Recode_Arr);
console.log("G1: " + Group1);
console.log("G2: " + Group2);
console.log("G3: " + Group3);

//create these fields in the survey flow at the beginning
Qualtrics.SurveyEngine.setEmbeddedData("QID1_Group1_Count", Group1);
Qualtrics.SurveyEngine.setEmbeddedData("QID1_Group2_Count", Group2);
Qualtrics.SurveyEngine.setEmbeddedData("QID1_Group3_Count", Group3);
});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
setTimeout(function () {jQuery('#NextButton').click();},0);
});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
jQuery(".JFE").show()
});

Userlevel 4
Badge +13

Use the code as below in onPageSubmit and then just set embedded data;

var Q1_val= [];
jQuery('input[type="checkbox"]').filter(":checked",true).each(function(){
   var val = jQuery(this).attr("choiceid")
   Q1_val.push(val)
})
var group1=0
if (Q1_val==1) {
group+=1;
}
if (Q1_val==2) {
group+=1;
}
if (Q1_val==3) {
group+=1;
}
Qualtrics.SurveyEngine.setEmbeddedData('Group1',group)

Userlevel 5
Badge +11

Hi grahulp5 ,

Thanks for the code. I struggled with ChoiceID as it doesn't follow the true button but just reports the position. Did this code work for you in that sense? I ask this because the question answers need to be randomised and when I was playing around it was just reporting the position, for example, I moved answer 3 up one level, it reported '2' instead of '3' as it's the 2nd button in the list. I need '3' to be reported
Let me know if it works for you as maybe there is something wrong with my setup.
In addition, please could you explain what .push and the plus part of += does in the code?
Thanks

Rod Pestell

Userlevel 4
Badge +13

Choice id is simply the id's formed at the backend in Qualtrics so its the best thing to get your selected statements as it won't change even if randomized. It does work for me;

.push simply pushes your selected values to the array Q1_val[] and += is simply an addition instead of a = a + 1, we use a+=1 which is the same thing.

Let me know if this works!!

Leave a Reply