call js function defined in header and pass arguments to it | XM Community
Skip to main content

Hello, there is a task I struggle to implement.
There are 22 fields, each input has to be compared to the ‘correctAnswer”, defined in embeddedData, and depending on the match/no match(there are some string manipulations to be done with the input before comparing) another embeddedData ‘score’, has to be assigned value 1 or 0.
 I want this function to be defined in the header, so it can be edited in one place.
something like 
 

function checkAnswer(questionId, correctAnswer, score){

}

can the function defined in the header, take embeddedData as an argument? Can I use How do I call the function ? Can I Qualtrics.SurveyEngine.setEmbeddedData('score1',score) in such function? (‘score1’ has to be passed as argument).
I am lost:(

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.


Leave a Reply