Use Javascript to set Embedded Data with addOnUnload | XM Community
Skip to main content

I want to use "Qualtrics.SurveyEngine.addOnUnload" to set embedded data with Javascript. I want to use "addOnUnload," though, so I can compare 2 values from questions in this block. Here is my script. The console.log returns the values I expect to see, but the embedded data is not set.
Note: In my survey flow, I have a "Set Embedded Data" block with the defined data before the block containing this script. I tried putting it after the block. That didn't help.
//set zip code data
var zipArray = [
"84003",
"84004",
"84005",
"84116"
]
var var1 = jQuery("#QID10").find(".InputText").val();
var placeholder = jQuery.inArray( var1, zipArray );
var saltLakeZip = ''; // extra step but helps with console.log troubleshooting.

if (placeholder != -1) {
saltLakeZip = 'yep'; 
Qualtrics.SurveyEngine.setEmbeddedData( 'saltLakeCityZip', saltLakeZip ); //Qualtrics embedded data

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

//set ticket data
var var2 = jQuery("#QID18").find(".InputText").val();
var theID = '';

if (var2.indexOf("Salt Lake City") !== -1 && saltLakeZip == 'yep') {
theID = 'SLC';
} else {
theID = 'AAA'
}

Qualtrics.SurveyEngine.setEmbeddedData( 'ticketID', theID ); //Qualtrics embedded data
console.log('ticket01 = ' + theID);

//console.log returns the correct values, but qualtics won't set teh embedded data.


If I use "Qualtrics.SurveyEngine.addOnload" with a blur on each question field, then the Javascript will set the embedded data, but that doesn't work for me because I want to compare the two values AFTER the user is done making changes and clicks the "Next" button.

AddonUnload is triggered when the page is unloaded, not when it starts unloading. So there are no values for your code to store.
You could look at addonPageSubmit or use the click on the next button as the trigger.


ahmedA
Doh! You are correct. I should have read the API closer. Instead of:
Qualtrics.SurveyEngine.addOnUnload()
I need to use:
Qualtrics.SurveyEngine.addOnPageSubmit()
Thank you for helping an overworked noob.


Leave a Reply