Skip to content

Instantly share code, notes, and snippets.

@marketinview
Last active November 27, 2023 17:57
Show Gist options
  • Save marketinview/aaf237fea91d2f7947fa4f170ac16486 to your computer and use it in GitHub Desktop.
Save marketinview/aaf237fea91d2f7947fa4f170ac16486 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");
}
});
@Shav-Ross
Copy link

God bless this author!!!

@marketinview
Copy link
Author

See additional Qualtrics solutions at: https://qualtricswiki.tgibbons.com/doku.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment