Hi everyone!
I'm fairly new to Qualtrics and JS. In my survey I ask respondents for their zip code and would like (a) turn responses to upper case and (b) to store their responses as embedded data. While I managed to do turn all responses to upper case, I did not manage to save the response as embedded data. I tried the following:
`Qualtrics.SurveyEngine.addOnload(function() {
var input = $("QR~" + this.questionId);
var text = PATH;
input.value = text.toUpperCase();
input.on('change', () => input.value = input.value.toUpperCase());
var newvar = input;
Qualtrics.SurveyEngine.setEmbeddedData('zipCode', newvar);
});`
Note: PATH stands for the precise location of ChoiceTextEntryValue.
With this code, I successfully turn responses to upper case, but the embedded data just says "array". If I use `var newvar = input.value` instead, the embedded data field is not filled. Any hints or recommendations are very welcome! Thanks a lot for your support in advance!
Page 1 / 1
Hello @sanm ,
Please follow the below Steps:
Step1: Create an embedded data(zipCode) in survey flow before the zipcode question
Step2: Paste the below code in the js(onReady) of the text entry zipcode question
jQuery("#"+this.questionId+" .InputText").on('keyup',function(){
Qualtrics.SurveyEngine.setEmbeddedData( 'zipCode', jQuery(this).val().toUpperCase());
});
Please follow the below Steps:
Step1: Create an embedded data(zipCode) in survey flow before the zipcode question
Step2: Paste the below code in the js(onReady) of the text entry zipcode question
jQuery("#"+this.questionId+" .InputText").on('keyup',function(){
Qualtrics.SurveyEngine.setEmbeddedData( 'zipCode', jQuery(this).val().toUpperCase());
});
Thanks a lot, @Shashi ! This was perfect! One last question: Ideally, I would like to remove any numerics from the zipcode response (e.g. "SW9" --> "SW", perhaps with `zipCode.replaceAll("[^A-Z]", "");`), group responses into regions (e.g. by using an if-else command). For instance:
`if(response === "SW"){
var region = 1;
} else {
var region = 0;
}`.
And then save the region variable as embedded data (i.e. not the actual zipcode but the region in which this zipcode is located).
Is there an elegant way to do that in Javascript? Many thanks again for the quick help!
`if(response === "SW"){
var region = 1;
} else {
var region = 0;
}`.
And then save the region variable as embedded data (i.e. not the actual zipcode but the region in which this zipcode is located).
Is there an elegant way to do that in Javascript? Many thanks again for the quick help!
> @sanm said:
> Thanks a lot, @Shashi ! This was perfect! One last question: Ideally, I would like to remove any numerics from the zipcode response (e.g. "SW9" --> "SW", perhaps with `zipCode.replaceAll("[^A-Z]", "");`), group responses into regions (e.g. by using an if-else command). For instance:
> `if(response === "SW"){
> var region = 1;
> } else {
> var region = 0;
> }`.
> And then save the region variable as embedded data (i.e. not the actual zipcode but the region in which this zipcode is located).
> Is there an elegant way to do that in Javascript? Many thanks again for the quick help!
Here is the updated code:
jQuery("#"+this.questionId+" .InputText").on('keyup',function(){
Qualtrics.SurveyEngine.setEmbeddedData( 'zipCode', jQuery(this).val().replace(/\\d+/g, '').toUpperCase());
});
> Thanks a lot, @Shashi ! This was perfect! One last question: Ideally, I would like to remove any numerics from the zipcode response (e.g. "SW9" --> "SW", perhaps with `zipCode.replaceAll("[^A-Z]", "");`), group responses into regions (e.g. by using an if-else command). For instance:
> `if(response === "SW"){
> var region = 1;
> } else {
> var region = 0;
> }`.
> And then save the region variable as embedded data (i.e. not the actual zipcode but the region in which this zipcode is located).
> Is there an elegant way to do that in Javascript? Many thanks again for the quick help!
Here is the updated code:
jQuery("#"+this.questionId+" .InputText").on('keyup',function(){
Qualtrics.SurveyEngine.setEmbeddedData( 'zipCode', jQuery(this).val().replace(/\\d+/g, '').toUpperCase());
});
Fantastic, thanks a lot again @Shashi !
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.