Hello, I am currently having issue getting this bit of code working. I am trying to take the average of four other embedded data variables, all of which are calculated during the survey.
I have four questions on one page that ask for the four values ("holdtoground1" etc.) to take the average of. Here is an example of the javascript for one of them:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var answer = document.getElementById('QR~' + this.questionId);
Qualtrics.SurveyEngine.setEmbeddedData( 'holdtoground3', answer.value );
});
On the fourth question, I also use javascript to calculate the average of these four variables ("holdtogroundBL"), which I think is where the issue is.
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var answer = (document.getElementById('QR~' + this.questionId)).value;
Qualtrics.SurveyEngine.setEmbeddedData( 'holdtoground4', answer );
//calculate overall hold to ground time
var item1 = parseInt("${e://Field/holdtoground1}");
var item2 = parseInt("${e://Field/holdtoground2}");
var item3 = parseInt("${e://Field/holdtoground3}");
var sum = item1 + item2 + item3 + answer.value
var average = sum / 4.0;
Qualtrics.SurveyEngine.setEmbeddedData( 'holdtogroundBL', average);
});
The problem is that I want to show the user that average on the next page, so after a page break I have displayed text that looks like this:
But, while the four smaller variables load, the average does not:
Can anyone tell me what my issue is? I made sure to set all the embedded data variables as Numbers, and even have them default to 0.
Page 1 / 1
Piped values are resolved on the survey before the page is sent to the browser. So, you can't set and pipe embedded data fields on the same page. You could get the values directly from the DOM (i.e., html page) to do the calculation.
Alternatively, you could calculate the average in the survey flow using a math expression:
holdtogroundBL = $e{ ( e://Field/holdtoground1 + e://Field/holdtoground2 + e://Field/holdtoground3 + e://Field/holdtoground4 ) / 4 }
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.