Last active
November 27, 2023 17:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
God bless this author!!!