Form Field - Character Limit | XM Community
Skip to main content

Hi Everyone

I am using this code successfully in an essay text box.

Qualtrics.SurveyEngine.addOnload(function()
{

jQuery("#"+this.questionId+" .InputText").attr("placeholder", "500 character limit.");
jQuery("#"+this.questionId+"-RemainingChars").css({"color":"#8c8c8c","font-size":"15px","visibility":"collapse"});
jQuery("#"+this.questionId+"-RemainingChars span").css({"visibility":"visible","float":"left"});

Which gives this me this.

How can I do the exact same but for a form field question with x2 fields?

 

Thanks :)

Hi @parkie_0007 ,

Try below code, it should work for form field.
 

Qualtrics.SurveyEngine.addOnload(function() {
var maxChars = 500;
jQuery("#" + this.questionId + " .InputText").attr("placeholder", "500 character limit.").each(function() {
var input = jQuery(this), display = jQuery("<div style='font-size:0.8em;'>Characters remaining: <span class='charCount'>" + maxChars + "</span></div>").insertAfter(input);
input.on("input", function() {
var remainingChars = maxChars - this.value.length;
if (remainingChars < 0) {
this.value = this.value.substr(0, maxChars);
remainingChars = 0;
}
display.find(".charCount").text(remainingChars);
});
});
});

 


Thanks @Deepak !

Really helpful!

  • How would I make the colour 8c8c8c?
  • How would I make the font-size 15px?
  • How would I show the remaining characters only after the 1st character is inputted.

My form question has x2 fields.

  • How would I make this applicable just for the 1st field?

Thanks! 🙏🏻


@parkie_0007 

Qualtrics.SurveyEngine.addOnload(function() {
var maxChars = 500;
jQuery("#" + this.questionId + " .InputText").eq(0).attr("placeholder", "500 character limit.").each(function() {
var input = jQuery(this),
display = jQuery("<div style='font-size:15px; color:#8c8c8c; visibility:hidden;'>Characters remaining: <span class='charCount'>" + maxChars + "</span></div>").insertAfter(input);

input.on("input", function() {
var remainingChars = maxChars - this.value.length;
if (remainingChars < 0) {
this.value = this.value.substr(0, maxChars);
remainingChars = 0;
}

if (this.value.length > 0) {
display.css('visibility', 'visible');
} else {
display.css('visibility', 'hidden');
}

display.find(".charCount").text(remainingChars);
});
});
});

 


Amazing, thanks :)


Sorry! @Deepak !

What if I wanted this again for the second field in the form?

Thanks :)


Leave a Reply