How to limit characters to just one free-text option on a multiple choice question? | XM Community
Skip to main content

Hello,
Sincere apologies if I post this question in the wrong place - I am totally new, though a long-time Qualtrics user!
I've worked out how to limit characters to text response questions and form field questions using Javascript as follows:

Qualtrics.SurveyEngine.addOnload(function()
{
jQuery("#"+this.questionId+" .InputText").each(function () { 
 jQuery(this).attr('maxlength','200');
});
});

Unfortunately, I cannot figure out how to prevent participants going over 200 characters on a multiple choice question where most of the items are clickable but only one is free-text. I believe it's possible to do this via 'matches regex', but I would rather participants can clearly see where they're unable to go any further. Presently, with the above code, they can select option 4 and then keep on writing beyond the 200 limit. Does anyone know how I can make this Javascript work for one free-text item within a multiple choice or a matrix table made up of other clickable options?

My questions are, for e.g., Q7:
Untitled.png
Q12:
Untitled2.png
I would be so grateful if someone could help me. Thank you, in advance!

Hi there, try adding the below to both questions' JavaScript in the OnReady section:
jQuery("#"+this.questionId+" .InputText").attr('maxlength','200');
Also, you might be interested in adding TomG's code from this thread which displays the character count back to the respondent.


Tom_1842, thank you so much - I'm so grateful for your time. I apologise, but I wonder if I'm doing something wrong, as the code doesn't seem to be working. Do you have any ideas? This is how I entered it for the first of the two questions above (multiple choice where they can choose multiple options but only the last has an additional free-text box that I want to limit. I changed '200' to '5' in the code below so I could test it easily).
1.png
I apologise sincerely if I'm doing something stupid - I'm really not a coder 😞
I did try TomG's code, but unfortunately I couldn't get that one working either! I only need a 'simple' version to stop participants writing any more when they reach the character limit, though.


https://community.qualtrics.com/XMcommunity/discussion/comment/53874#Comment_53874Use:
jQuery("#"+this.questionId+" .TextEntryBox").attr('maxlength','5');


Oh interesting, it looks like when the Text Entry is set to Medium or Large it no longer has the InputText class. TomG's code should work for you.


Thank you both so much, truly! I am so grateful for your taking the time to help me. TomG's code worked 😊
(I'm glad I wasn't missing something obvious! 😅 )


jQuery("#"+this.questionId+" .TextEntryBox").attr('maxlength','5');

 

I have a current project that needs precisely this code for a character limit. However, I can’t seem to make this function in that particular survey. If I create a new/blank/generic survey and MC question, this code works flawlessly as others have mentioned. We do have choice randomization enabled for that question, but adding advanced randomization to a blank project/question doesn’t have the same issue.

Help! 😂 Has anyone run into this kind of issue? If so, were you able to identify any other survey settings, etc. that were somehow preventing this script from functioning correctly?


To follow up on my prior post, it seems the issue I am running into was due to the original instrument utilizing the simple layout, which does not automatically support the ability to call the jquery library. Unfortunately, I have not yet found a way to call the library from the look and feel header in the simple layout either.


Leave a Reply