Javascript Array Determines Branch | XM Community
Skip to main content

I ask my user for a zip code. If it matches any of the zip codes in my Javascript array, then I want to use a branch.

  • If zip matches a value in my array, then go to block 2 when the block 1 "Next" button is pressed.

  • If zip does NOT match a value in my array, then go to block 3 when the block 1 "Next" button is pressed.

My question is very similar to this post, but I don't understand what TomG is saying in the second part of his answer. I hope someone can help give me some example script.
Thank you for helping an overworked noob. :)

In the JS logic, you would set the embedded data then based on the embedded data you determine the subsequent branching.
Qualtrics JavaScript Question API - Qualtrics Question API


pogi
Thank you for taking the time to read my post. That helps, but I'm still not getting it. I was hoping someone could provide some sample Javascript. For example, my zip code field is a text field. Do I need to add an onChange trigger or similar?
I think this is a start, but I don't know how to get the value of the text field.
var q = jQuery("#"+this.questionId);
q.find(".InputText").on("blur", function(e) {
  console.log(what goes here?);
});


After reading posts from pogi and TomG , I came up with the following. This gets the job done, but I wonder if there is a more concise or elegant way to make it work.
The setEmbeddedData part comes from AnthonyR on this post.
I added the script to: Qualtrics.SurveyEngine.addOnload(function() {
//I want to find out if the user enters a zip code that matches one of my Salt Lake City codes in an array.
var zipArray = [
"84003",
"84004",
"84005",
"84010",
"84664"
]


var q = jQuery("#"+this.questionId);
var theZip = null; //what did the user enter? changes on blur
var saltLakeZip = false; //is this an SLC zip?


// when the input text blurs, do something
q.find(".InputText").on("blur", function(e) {
theZip = q.find(".InputText").val();
//console.log("theZip = " + theZip);

var zipPlaceholder = jQuery.inArray( theZip, zipArray ); //is theZip in my array? For example, 84003 is there so it won't result in "-1"

if (zipPlaceholder != -1) {
//console.log('there was a match');
saltLakeZip = true;
Qualtrics.SurveyEngine.setEmbeddedData( 'saltLakeCityZip', "yes" ); //saltLakeCityZip is Qualtrics embedded data

} else {
saltLakeZip = false;
Qualtrics.SurveyEngine.setEmbeddedData( 'saltLakeCityZip', "no" ); //saltLakeCityZip is Qualtrics embedded data
}
//console.log("saltLakeZip = " + saltLakeZip);
});



Leave a Reply