Yes we need to define the function in the header under script tag and then we can call that function from any question JS.
Dear @Shashi I am stuck on how to implement it:( let say I have function which is alert userInput, and embbededValues, and also set one of the embeddedValues to100. Nothing happens:( This is how the function looks in the header:
function testFunc(userInput, answerString, score ){
a=Qualtrics.SurveyEngine.getEmbeddedData(answerString);
Qualtrics.SurveyEngine.setEmbeddedData(score,100);
alert(userInput);
alert(a);
alert(score);
}
and then in the survey, I am calling this function like this:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
testFunc(userInput=$('.InputText').val, answerString='rightAnswer1', score='score1');
});
rightAnswer1 and score1 are set in survey flow as embeddedData.
I know I do something fundamentally wrong:(
@nonufrie,
Corrected:
<script>
function testFunc(userInput, answerString, score ){
var a=Qualtrics.SurveyEngine.getEmbeddedData(answerString);
Qualtrics.SurveyEngine.setEmbeddedData(score,100);
alert(userInput);
alert(a);
alert(score);
}
</script>
and
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var input = jQuery("#"+this.questionId+" .InputText");
testFunc(input, 'rightAnswer1', 'score1');
});
@TomG thank you so, so much! All worked, he only adjustment was that I added .val() to
var input = jQuery("#"+this.questionId+" .InputText").val();
But now I feel like this line should be the part of the function, since it’s the same calls:
<script>
function testFunc(answerString, score ){
var a=Qualtrics.SurveyEngine.getEmbeddedData(answerString);
var userInput= jQuery("#"+this.questionId+" .InputText").val();
Qualtrics.SurveyEngine.setEmbeddedData(score,100);
alert(userInput);
alert(a);
alert(score);
}
</script>
But when I do that alert(userInput) brings up ‘undefined’. What do I need to adjust inside that function?
@nonufrie,
That’s because the question object (this) is local to the addOnPageSubmit function. You can find the .InputText fields on the page from the function, but it/they wouldn’t be tied to a specific question. The more flexible approach is to pass something related to the field of interest (question object, question id, input element, or value) as a function argument.
@TomG thank you so, so much for your help, I got it all working.