How to count the number of non-empty responses in a Form type Text Entry question? | XM Community
Skip to main content

I have a text entry question with a large number of Form type items.
I would like to find a way to create an embedded data variable that is a count of how many of these Form items were filled out (i.e., not left empty).
Is there a way to do this?

Create an embedded data - "Count" before the form type question in survey flow. Paste the below code in the JS of the form type question:
Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
if(type == "next")
{
var c = 0;
jQuery("#"+this.questionId+" .InputText").each(function(){

if(jQuery(this).val().trim()!="") c++;
});
Qualtrics.SurveyEngine.setEmbeddedData("Count", c);
}
});


Thanks so much! This seems like a perfect solution, but I'm obviously struggling with something.
Does "this.questionId" have to be changed to my own question ID or something?
When I pipe in the embedded data value on a later page, it shows as 0 every time, regardless of how many form fields I filled in.
Not sure what I'm doing wrong?


https://www.qualtrics.com/community/discussion/comment/30893#Comment_30893Please send the screenshot of where you have pasted the code.


Qualtrics.SurveyEngine.addOnload(function()
{
{
jQuery("#"+this.questionId+" .InputText").focus( function() {
var isInputText = document.activeElement instanceof HTMLInputElement && document.activeElement.type == 'text';

  console.log(isInputText);

  if (isInputText)
  {
    tbody2 = document.activeElement.parentNode.parentNode.nextSibling.nextSibling.show()//style.display = 'inline-block';
  }
});

var initialFields = 5;

var choices = this.getChoiceContainer();   

//choices.hide();

//alert(choices);

var tbody = choices.getElementsByTagName('tbody');
//alert(tbodyb0].childNodes.length);

childs = tbodyr0].childNodes;


for (i=(initialFields*2); i {
  if (childs}


this.questionclick = function(event,element)
{
  var isInputText = element instanceof HTMLInputElement && element.type == 'text';


  if (isInputText)
  {
    tbody2 = element.parentNode.parentNode.nextSibling.nextSibling.show()//style.display = 'inline-block';
  }
}
};

Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
if(type == "next")
{
var c = 0;
jQuery("#"+this.questionId+" .InputText").each(function(){

if(jQuery(this).val().trim()!="") c++;
});
Qualtrics.SurveyEngine.setEmbeddedData("Count", c);
}
});

});

Qualtrics.SurveyEngine.addOnReady(function()
{

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});


I've pasted all the JS for that question.
As you can see, I have another little piece of JS that makes it so that only 5 text forms display, but when you click on the nth form box, another form box appears.
I wonder if something from that other bit of script is somehow fouling things up?


Paste the code after closing of onUnload function as seen in below screen shot:
image.png


Oh man, works like a charm! Thank you so much for this fix. You saved my evening! :)


Leave a Reply