Count number of choices in a given question with dynamic number of choices. | XM Community
Skip to main content
Solved

Count number of choices in a given question with dynamic number of choices.

  • September 1, 2023
  • 2 replies
  • 150 views

Forum|alt.badge.img+5
  • Level 3 ●●●
  • 17 replies

Hi!

 

I am trying to create a variable that counts the number of choices that currently exist in my question to make a loop, as I need to browse the possible choices to evaluate if they have been checked and set them to a specific value. The options are dynamic, as they appear depending on whether a YES was given in two different previous questions. I think I could also get this value if I use the If else option, but I wanted to know if there is a possibility to do it with only one line of code and not 6 or 7. Here below is my code for more context of what I tried (numSelectableOptions):
 

Qualtrics.SurveyEngine.addOnPageSubmit(function() {

var  x =	jQuery("#" + this.questionId + " input[type=text]").eq(0).val();
var  y=		jQuery("#" + this.questionId + " input[type=text]").eq(1).val();
var  z =	jQuery("#" + this.questionId + " input[type=text]").eq(2).val();

var numSelectableOptions = jQuery("#" + questionId + " .q-checkbox:not(.q-disabled)").length;

  // Loop through each item and check if it's not empty (embedded variable has a value)
  for (var i = 1; i <= numSelectableOptions; i++) {
    var itemValue = Qualtrics.SurveyEngine.getEmbeddedData("Inha" + i); // Get the embedded variable value for each item
	  // If the itemValue is not empty (not equal to an empty string)

	  if (itemValue == "other specify (1):") {
Qualtrics.SurveyEngine.setEmbeddedData("Inha"+i, x);	
    }
     if (itemValue == "other specify (2):") {
Qualtrics.SurveyEngine.setEmbeddedData("Inha"+i, y);	
    }
      if (itemValue == "other specify (3):") {
Qualtrics.SurveyEngine.setEmbeddedData("Inha"+i, z);	
    }
 }


});

And I also tried this:

var numChoices = jQuery('#' + questionId + ' .q-radio').length;

Nothing above works.

Thanks in advance for any suggestions.

Regards!

 

 

Best answer by Josang

I close this post. 

I did not realize that I did not put this.questionID and left “questionID” only. It works fine.

View original

2 replies

Forum|alt.badge.img+5
  • Author
  • Level 3 ●●●
  • 17 replies
  • Answer
  • September 1, 2023

I close this post. 

I did not realize that I did not put this.questionID and left “questionID” only. It works fine.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5938 replies
  • September 1, 2023

I’m not clear on what you are trying to accomplish, but you can loop through the choices once and get what you need:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
	var qobj = this;
	jQuery.each(qobj.getChoices(), function(i, cid) {
		console.log("Loop",i); // the loop index)
		console.log("Choice Id",cid); // the choice id
		console.log("Value",qobj.getChoiceValue(cid)); //true if selected, otherwise false
		console.log("Displayed",qobj.getChoiceDisplayed(cid)); //true if displayed, otherwise false
		console.log("Text Value",qobj.getTextValue(cid)); //the associated text entry, blank if none
	});
});

 


Leave a Reply