Issue with JavaScript Not Functioning Properly in Qualtrics Survey Preview | XM Community
Skip to main content
Solved

Issue with JavaScript Not Functioning Properly in Qualtrics Survey Preview


Forum|alt.badge.img+2

I wrote a JavaScript to randomly draw 2 numbers in a question with some constraints. The script works when I select the question box and click “Preview Question”, as shown below

but it does not function properly when I click preview for the survey, as shown below

 

Here is the code I wrote. Could anyone tell me why this issue occurs and provide some guidance? I would really appreciate your help! Thanks a lot in advance!!!

 

Best answer by Chee Heng_SZ

Hi ​@w29913 ,

I am not very knowledgeable on JavaScript. I used the piped text of the form field question instead.

Then, i realised that they are concatenating when I tried to add them.

Hence, I changed the data type of value1 and random1, which are likely string type, to numbers before adding them. 

Qualtrics.SurveyEngine.addOnload(function()
{
	/*Place your JavaScript here to run when the page loads*/
	const random1options=[0,2,4,6];
	const random2options=[0,20,30,40];

	let random1=random1options[Math.floor(Math.random() * random1options.length)];
	let random2=random2options[Math.floor(Math.random() * random2options.length)];

	while (random1 === 0 && random2 ===0) {
	random1=random1options[Math.floor(Math.random() * random1options.length)];
	random2=random2options[Math.floor(Math.random() * random2options.length)];
	}

    /*For checking data in console. To delete if you do not require this line.*/
	console.log("Random1: " +random1); 

    /*Getting the 2nd input of the form field question. Do remember to change the QID to suit your case*/
	let value1= "${q://QID20/ChoiceTextEntryValue/2}";
    /*Changing the format of the value1 and random1 from string to number.*/
	let sum= Number(value1)+ Number(random1);

    /*For checking data in console. To delete if you do not require these two lines.*/
	console.log("value1: " +value1);
	console.log("sum: " +sum);

	this.getQuestionTextContainer().innerHTML="xx increase by " + sum +" and xx increase by "+random2 +"%, which of the following you will choose?";
});

 

View original

4 replies

Forum|alt.badge.img+16

Try changing the portion from ` to “” instead?

this.getQuestionTextContainer().innerHTML=
"xx increase by " + random1 +" and xx increase by "+random2 +" %, which of the following you will choose?";

 


Forum|alt.badge.img+2
  • Author
  • 3 replies
  • March 5, 2025
Chee Heng_SZ wrote:

Try changing the portion from ` to “” instead?

this.getQuestionTextContainer().innerHTML=
"xx increase by " + random1 +" and xx increase by "+random2 +" %, which of the following you will choose?";

 

@Chee Heng_SZ I tried, it works perfectly, thank you so much! 


Now I want to retrieve the value that participants enter in the previous question, question type is form field with two input fields. I want to extract the value from the second field and add this number to random 1, and use the sum to display in the question. I tried the following code, but it doesn’t work, would you be kind enough to provide some guidance again?

 

Many thanks for your help in advance!!!


Forum|alt.badge.img+16

Hi ​@w29913 ,

I am not very knowledgeable on JavaScript. I used the piped text of the form field question instead.

Then, i realised that they are concatenating when I tried to add them.

Hence, I changed the data type of value1 and random1, which are likely string type, to numbers before adding them. 

Qualtrics.SurveyEngine.addOnload(function()
{
	/*Place your JavaScript here to run when the page loads*/
	const random1options=[0,2,4,6];
	const random2options=[0,20,30,40];

	let random1=random1options[Math.floor(Math.random() * random1options.length)];
	let random2=random2options[Math.floor(Math.random() * random2options.length)];

	while (random1 === 0 && random2 ===0) {
	random1=random1options[Math.floor(Math.random() * random1options.length)];
	random2=random2options[Math.floor(Math.random() * random2options.length)];
	}

    /*For checking data in console. To delete if you do not require this line.*/
	console.log("Random1: " +random1); 

    /*Getting the 2nd input of the form field question. Do remember to change the QID to suit your case*/
	let value1= "${q://QID20/ChoiceTextEntryValue/2}";
    /*Changing the format of the value1 and random1 from string to number.*/
	let sum= Number(value1)+ Number(random1);

    /*For checking data in console. To delete if you do not require these two lines.*/
	console.log("value1: " +value1);
	console.log("sum: " +sum);

	this.getQuestionTextContainer().innerHTML="xx increase by " + sum +" and xx increase by "+random2 +"%, which of the following you will choose?";
});

 


Forum|alt.badge.img+2
  • Author
  • 3 replies
  • March 7, 2025
Chee Heng_SZ wrote:

Hi ​@w29913 ,

I am not very knowledgeable on JavaScript. I used the piped text of the form field question instead.

Then, i realised that they are concatenating when I tried to add them.

Hence, I changed the data type of value1 and random1, which are likely string type, to numbers before adding them. 

Qualtrics.SurveyEngine.addOnload(function()
{
	/*Place your JavaScript here to run when the page loads*/
	const random1options=[0,2,4,6];
	const random2options=[0,20,30,40];

	let random1=random1options[Math.floor(Math.random() * random1options.length)];
	let random2=random2options[Math.floor(Math.random() * random2options.length)];

	while (random1 === 0 && random2 ===0) {
	random1=random1options[Math.floor(Math.random() * random1options.length)];
	random2=random2options[Math.floor(Math.random() * random2options.length)];
	}

    /*For checking data in console. To delete if you do not require this line.*/
	console.log("Random1: " +random1); 

    /*Getting the 2nd input of the form field question. Do remember to change the QID to suit your case*/
	let value1= "${q://QID20/ChoiceTextEntryValue/2}";
    /*Changing the format of the value1 and random1 from string to number.*/
	let sum= Number(value1)+ Number(random1);

    /*For checking data in console. To delete if you do not require these two lines.*/
	console.log("value1: " +value1);
	console.log("sum: " +sum);

	this.getQuestionTextContainer().innerHTML="xx increase by " + sum +" and xx increase by "+random2 +"%, which of the following you will choose?";
});

 

Again, it works! Thank you so much for your help ​@Chee Heng_SZ and for pointing out the data type issue!


Leave a Reply