Solved
How do I show the number of words in text entry boxes? I have several on the page.
How do I show the number of words in text entry boxes? I have several on the page.
Best answer by AnthonyR
Just got this worked out.
Create a text entry type question.
Create a span with an ID of wordCountDisplay in your question text, give it a default value of 0. Add this to your question's html.
For instance:
`Your word count is: <span id='wordCountDisplay'>0</span>`
Then add the following to the JavaScript editor for this queston:
Qualtrics.SurveyEngine.addOnReady(function()
{
var display = $('wordCountDisplay');
var questionID = this.questionId;
var textbox =$('QR~' + questionID);
var that = this;
function countWords(s){
s = s.replace(/\\n/g,' '); // newlines to space
s = s.replace(/(^\\s*)|(\\s*$)/gi,''); // remove spaces from start + end
s = s.replace(/[ ]{2,}/gi,' '); // 2 or more spaces to 1
if(s == ''){
return 0;
}else{
return s.split(' ').length;
}
}
textbox.onkeyup = function(e){
display.update(countWords(textbox.value));
console.log(textbox.value);
}
});
This will update dynamically as the respondent types in to the text box.
I have attached a qsf demo
View originalLeave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.