Can I count lines of text in a text entry box? | XM Community
Skip to main content

Hi there! I've been building an experiment on Qualtrics. Basically I am looking for participants to repeat a phrase a number of times and I'd like to count the number of times they retype that one phrase. I had originally done this through a text box, but that would leave me to count it manually after the experiment, which isn't ideal. So I have three possible solutions that make sense in my head, but I'm not sure if Qualtrics would allow me to use any of them.

  1. Counting the number of times a phrase is repeated. I don't like this option because I want participants to be able to make a few mistakes in their typing and have it still counted.

  2. Having the participants press "enter/return" and move to a new line in the text box with each repetition, and then counting the total number of lines used. This feels the simplest to me in terms of what I've already built, but I'm concerned may only be solved through JS, which I don't have experience with.

  3. Using a Form Field instead of a Text box, but displaying each new field while the page is still active whenever a form is filled out. I'm not familiar with form field and how to use that, but have been reading about it. I haven't found that option in the display logic, but maybe I'm missing something.

Okay, that's what I've got. I'd really appreciate any help with making the three options I have work well, or any other suggestions I may not be aware of.
Thanks so much!

Yes, for (2) you can add the following to the question:
Qualtrics.SurveyEngine.addOnPageSubmit() {
var lines = jQuery("#"+this.questionId+" .InputText").val().split("\\n");
Qualtrics.SurveyEngine.setEmbeddedData("count",lines.length);
});
Add 'count' (or whatever name you choose) as an embedded data field at the beginning of the survey flow so it gets saved in the response data.


Hi TomG, thank you for your reply! I tried it and it worked generally. I forgot to mention that I'm using a loop to present each variation of the phrase in the same block. The way that this syntax is working only seems to produce an output of either the average of all the submissions or only the last submission of the block. Is there a way to make this output count separately for each iteration of the loop?


https://community.qualtrics.com/XMcommunity/discussion/comment/47588#Comment_47588Do something like this:
Qualtrics.SurveyEngine.addOnPageSubmit() {
var lines = jQuery("#"+this.questionId+" .InputText").val().split("\\n");
Qualtrics.SurveyEngine.setEmbeddedData("count_${lm://CurrentLoopNumber}",lines.length);
});


Hi TomG, I appreciate your response again! I'm having trouble getting a count result out of it; I've tried a number of variations. Could you maybe explain what you mean a little bit more? I'm still pretty new to this so may be missing something. Sorry!


https://community.qualtrics.com/XMcommunity/discussion/comment/47592#Comment_47592You need a separate count for each loop, so the code will save embedded fields count1, count2, etc. You'll need to add all those fields to the survey flow to save them in the response data. This assumes you haven't randomized the loop. If you have randomized the loop you'll need to use a loop & merge field instead of current loop for 1, 2, etc.


Great, I got it working! Thank you so much!


Leave a Reply