How to create variable based on previous response? | XM Community
Solved

How to create variable based on previous response?

  • 19 September 2018
  • 3 replies
  • 41 views

I've never used javascript.

I'm going to be doing a lot of display logic based on zipcode. Essentially, I want to take a lot of zipcodes (over 1k) and condense them to a couple dozen.
My plan was to create a hidden question and create some if logic to condense.

So - Q1.4 is where I ask people to enter zipcode
Q72 is a hidden question where I hope to condense the codes. From poking around on here, I've got:

jQuery("#"+this.questionId).hide(); //here's where I'm hiding the question

Which works perfectly... but I haven't been able to just do some simple logic like: if zipcode is '60606' then store '33' into Q72.

It seems like this should be really easy.
icon

Best answer by SaurabhPujare_Ugam 19 September 2018, 18:36

View original

3 replies

Userlevel 3
Badge +1
Using if conditions in JavaScript punch in hidden question like you may want to punch region based on the zip code.

If(pipe zip variable== code) this.question.set(33).

Something like this.
Userlevel 6
Badge +18
Hey @Dan,

Hope it helps...

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

var tboxID= "QR~"+this.questionId;
var val = document.getElementById(tboxID).value;

Qualtrics.SurveyEngine.setEmbeddedData("Val",val);

if(val==60606)
{
Qualtrics.SurveyEngine.setEmbeddedData("ZipCode",33);
}
});
Userlevel 7
Badge +27
@Dan,

Since you have a large number of zip codes you need to map, my preference would be to store them in an external database table, then call a web service to do a lookup and return the associated code as an embedded variable.

To answer your question of how to do it in JavaScript, first define an object with all your zip codes:
```
var zips = {
60606: "33",
etc. (999 more zip codes)
};
```
Then pipe your zip code answer into the script to lookup and set the value of the text input in your hidden question (change QID accordingly):
```
jQuery("#"+this.questionId+" .InputText").val(zips["${q://QID4/ChoiceTextEntryValue}"]);
```

Leave a Reply