How to create variable based on previous response? | XM Community
Skip to main content
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.
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.
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);

}

});
@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