Counting Number of Non-Empty Text Responses Using JavaScript | XM Community
Skip to main content

I have a text entry question that is in a block of my survey that is looped. Is it possible to use JavaScript to set an embedded data field that gives a count of the number of non-empty responses there are for that question?

Instead of JS, we can use branch logic and Math operation to set the embedded data.


@rondev The branch + math approach works great if the survey is relatively simple, but how would one approach it if the survey was too large and already branched with multiple layers of logic to add in a branch every time you wanted to count text boxes? It seems like there must be a relatively straightforward way to do this in js by incrementing on an ongoing count field on each page submit based on the number of qids that are submitted ‘not empty,’ but I haven’t been able to find an example anywhere, despite what seems like threads going back years of people looking for a way to count how many text entry questions have been answered. 

As a non-code, non-branching fallback, I could create a hidden question that lists every text box in the survey, then use answer choice display logic to display if not empty, and check all displayed answers with js. I can do that, but it means making a list of about 1000 items since each loop’s questions would need to be done individually. I’d really rather find a way to increment a count in js and keep it in a field instead of adding another thousand columns to the dataset (it’s already too many columns to view it in the Data & Analysis tab, and I can’t download in Chrome because of header limits, so I already have to open Firefox to download the data each day). Any suggestions or pointers to tutorials/starting points/sample code would be greatly appreciated. 

Thanks!


Never mind, I figured out a way by cobbling together pieces of code from other related scenarios. :) 

 


Leave a Reply