Updating an embedded variable from text field | XM Community
Skip to main content

I searched and found this discussion (and a couple others) but the solutions are not working for me: https://www.qualtrics.com/community/discussion/199/using-javascript-with-embedded-data-and-or-selected-choices
I am using Qualtrics to collect data on an experiment related to behavioral economics.
I need to provide respondents with a budget and then update the budget throughout the survey as they spend it.
I have coded up a toy example to try to get the javascript right here. I am having trouble updating the budget after they spend it in a question.
Here are the questions and the underlying javascript. Can anyone identify where I am going wrong?
Step 0: I set the embedded data field "Budget" to $100 at the beginning of the survey.
Step 1: I display the first page.
*Q1: Your budget is ${e://Field/Budget} This displays correctly.
*Q2: How much to spend? (Open text field, validated as a number.)
Javascript:
Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
if(type == "next")
{
var prevbudget = Qualtrics.SurveyEngine.getEmbeddedData("Budget");
var prevbudget = parseInt(prevbudget);
console.log(prevbudget); //100
//var temp = Qualtrics.SurveyEngine.getTextValue("QID2"); //creates an error
var temp = "${q://QID2/ChoiceTextEntryValue}";
console.log(temp); //null
var temp = parseInt(temp);
var budget = prevbudget-temp;
Qualtrics.SurveyEngine.setEmbeddedData('Budget', budget);
}
});
Step 2: I display the next page with updated info.
*Q3: Your budget it ${e://Field/Budget}. not displayed
You previously spent ${q://QID2/ChoiceTextEntryValue}. Displays correctly
*Q4: How much to spend? (Open text field, validated as a number.)
...ideally the process continues with more questions asking for spending and displaying budgets.
It seems to me I am getting something wrong with my understanding of the Qualtrics API or the javascript. Any ideas?

Try replacing the temp value by setting it through JavaScript/jQuery instead of piped text.

var temp = jQuery("[id='QR~QID2']").val(); //replace the ID as required

Hope this should work for you.


Yes, the jQuery route does work. I ended up coding it as:
var temp = jQuery("#"+this.questionId+" .InputText").val();
I think this.questionId will prevent me from accidentally breaking it if I change the question order or ID number along the way.


Leave a Reply