Solved
How to add a string counter in the Qualtrics
Hello, I want to add a counter which could count the number of sub-string "ab" and "ba". That is , participants type letters in a text bar, like "ababaaaabbbbba", and I want to count the number of "ab" or "ba" and show it on the screen on Qualtrics. In the example, there are 3 "ab" or "ba" in this string, so the he or she will see "3" on the screen. I will be appreciate if anyone can tell me how to write the Javascript.
Best answer by Anonymous
Hello @Anna999 ,
Please follow the below steps:
Step 1: Create two embedded variables ( eg: ab_freq, ba_freq)i.e one for frequency of 'ab' and another for frequency of 'ba' and set there values as 0.
Step 2: Paste the below code in the js(onReady) of Text entry question type.
var that =this.questionId;
jQuery("#"+that+" .InputText").on('blur',function(){
var temp = this.value;
var count = (temp.match(/ab/g) || []).length;
Qualtrics.SurveyEngine.setEmbeddedData( 'ab_freq', count );
count = (temp.match(/ba/g) || []).length;
Qualtrics.SurveyEngine.setEmbeddedData( 'ba_freq', count );
});
Step 3: The above code will set the number of occurence of 'ab' in "ab_freq" embedded variable and of 'ba' in "ba_freq" embedded variable.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.

!
!
Thank you for your reply. I have tried to do as above, but it doesn't work, could you tell how to revised it?
Also declare the two embedded data as first element in the survey flow and also set there values as 0
There should be a page break between the question where the string is asked and the question where the frequency is displayed.