Skip to content

Instantly share code, notes, and snippets.

@joonro
Created August 2, 2018 18:02
Show Gist options
  • Select an option

  • Save joonro/387922efbe37d5f5735de1b013e2a7da to your computer and use it in GitHub Desktop.

Select an option

Save joonro/387922efbe37d5f5735de1b013e2a7da to your computer and use it in GitHub Desktop.
[Show Number of Words After Textbox] #qualtrics
Qualtrics.SurveyEngine.addOnReady(function()
{
var questionID = this.questionId;
var textbox =$('QR~' + questionID);
textbox.insert({after: '<br><span style="font-size: 14.4px;">Your word count is:&nbsp;</span><span id="wordCountDisplay">0</span>'}); var display = $('wordCountDisplay');
var display = $('wordCountDisplay');
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);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment