Word or Character Count Trigger | XM Community
Skip to main content
Hello all! I want to create an indicator if an open-ended feedback response contains 300 or more characters. I know I can use branching logic to create an embedded data field for this indicator, but I am not sure how to set up the trigger. Is this something I'd need custom code to do?
Yes, you would need to do this in JavaScript. Do you want to display the indicator to the respondent as soon as they hit 300 characters or do you just want a flag after the fact? If it is the former you need an input event listener. If it is the later you can just get the string length in an onPageSubmit function.
Awesome, thanks @TomG . Fortunately it is the later. Essentially we just want to know when someone types a long response as it might be an indicator of a bad experience.



Would the easiest way to do this be "PUT" string length to Embedded Data "Comment Length" then use Branch logic to check if "Comment Length" is greater than/equal to 300?
You could use setEmbeddedData to save the length or just set a flag (e.g. LongComment = 1), it is just a matter of preference.
@TomG - Thank you. That makes sense. I'll need to look in to what that code would look like. I probably need something like "count", then "if count >299" setEmbeddedData "LongComment = 1". I am still a JavaScript novice, but I keep finding use cases that justify me learning more.
@TomG - I'm new at JavaScript. Would you mind sharing the script to use for word counting in a text entry field and store this info as embedded data ?



Thanks a lot !
@TomG @VinceLef @Akdashboard -- trying to do the same thing and not familiar with JavaScript. How do you create the embedded data flag for a long comment? It's exactly what we are trying to do with some key detractor feedback. Thanks! Emily
Hi @emilyvoc , here's the Java Script I added to my open-end question:



Qualtrics.SurveyEngine.addOnReady(function()

{

var questionID = this.questionId;

var textbox =$('QR~' + questionID);

var that = this;

function countWords(s){

s = s.replace(/\\n/g,' '); // newlines to space

s = s.replace(/(^\\s*)|(\\s*$)/gi,''); // remove spaces from start + end

s = s.replace(/[ ]{2,}/gi,' '); // 2 or more spaces to 1

if(s == ''){

return 0;

}else{

return s.split(' ').length;

}

}

textbox.onkeyup = function(e){



var aa = countWords(textbox.value);

console.log(textbox.value);

Qualtrics.SurveyEngine.setEmbeddedData("ED1", aa);



}



});



From what I understand, as I'm new to this, is you need to update the last part of the script . "aa" needs to unique to each open-end question as well as "ED1" which is the name of your new embedded data in your dataset.



Once this is done, you need to map your embedded data in your survey flow (Add embedded data and type the name of your ED variable - make sure it is in number format).



Happy to help further if needed.



Good luck !

Hi @emilyvoc , here's the Java Script I added to my open-end question:

Qualtrics.SurveyEngine.addOnReady(function()
{
var questionID = this.questionId;
var textbox =$('QR~' + questionID);
var that = this;
function countWords(s){
s = s.replace(/\\n/g,' '); // newlines to space
s = s.replace(/(^\\s*)|(\\s*$)/gi,''); // remove spaces from start + end
s = s.replace(/[ ]{2,}/gi,' '); // 2 or more spaces to 1
if(s == ''){
return 0;
}else{
return s.split(' ').length;
}
}
textbox.onkeyup = function(e){

var aa = countWords(textbox.value);
console.log(textbox.value);
Qualtrics.SurveyEngine.setEmbeddedData("ED1", aa);

}

});

From what I understand, as I'm new to this, is you need to update the last part of the script . "aa" needs to unique to each open-end question as well as "ED1" which is the name of your new embedded data in your dataset.

Once this is done, you need to map your embedded data in your survey flow (Add embedded data and type the name of your ED variable - make sure it is in number format).

Happy to help further if needed.

Good luck !

 

@VinceLef any idea how you would count the number of characters (including spaces) in an embedded data field instead of an open end question?  I figured out how to use Regex coding to see if the embedded data field is greater than the character limit I want, but having trouble getting an actual count.


Leave a Reply