Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save georggr/82dddd3ed5d48d40660679837116749e to your computer and use it in GitHub Desktop.
Save georggr/82dddd3ed5d48d40660679837116749e to your computer and use it in GitHub Desktop.
Qualtrics: Display count of words in a text entry box. Option to limit number of words. #qualtrics #js #jq #text #count #word #limit
Qualtrics.SurveyEngine.addOnload(function() {
var maxWords = 0; //update as needed, 0=no limit
var q = jQuery("#"+this.questionId);
var input = q.find(".InputText");
input.after("<div style='font-size:0.8em;float:right'>Word count: <span class='wordCount'>0</span><span class='maxWords'></span></div>");
var display = q.find(".wordCount");
if(maxWords > 0) q.find('.maxWords').text("/"+maxWords);
countWords(input.get(0));
input.on("input", function() { countWords(this) });
input.blur(function() { this.value = this.value.trim(); });
function countWords(el) {
if(el.value.length > 0) {
if(maxWords > 0) {
if(el.value.match(/\S+/g).length > maxWords) el.value = el.value.replace(/(\s+)\S+$/,"$1");
}
display.text(el.value.match(/\S+/g).length);
}
else display.text("0");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment