Javascript: Embedded Data doesn't update | XM Community
Skip to main content
Question

Javascript: Embedded Data doesn't update

  • October 25, 2023
  • 3 replies
  • 144 views

Forum|alt.badge.img+1

I have been trying to run a very simple js code on Qualtrics for about 5 hours now.

 

There are 3 blocks:

  • Block 1 captures a numeric value (say p)
  • Block 2 creates an embedded value (say embedVar =-99)
  • Block 3 captures another numeric value (say r)
  • Within Block 3, on a new page, I want to display the embedded variable embedVar = r/p

For this I am running the following js code:

	var p ="${q://QID7/ChoiceTextEntryValue}" ;
var r = "${q://QID2/ChoiceTextEntryValue}" ;
var result = Math.ceil(r/p) ;
Qualtrics.SurveyEngine.setEmbeddedData( 'embedVar', result );

This just does not work. I have tried to put it on “onload”, “onunload”, “onsubmit”, “onready” (please don’t mind the case-insensitivity). I have put this code in the page where I record the response r, the page where I am supposed display the updated embedVar. I exhausted every combination. It just remains -99 or it shows a 0.

 

What does work is if I equate embedVar to p, it does show the actually recorded p. This makes me suspect that the js is not running at the appropriate time. Can anyone please help? It is a very simple code that is supposed to work, but it just doesn’t to work.

 

To note: The block 3 runs a loop where, in every loop, the value p is recorded and I try to update the embedVar and try to display it. 

3 replies

Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+29
  • QPN Level 8 ●●●●●●●●
  • 1096 replies
  • October 25, 2023

I have been trying to run a very simple js code on Qualtrics for about 5 hours now.

 

There are 3 blocks:

  • Block 1 captures a numeric value (say p)
  • Block 2 creates an embedded value (say embedVar =-99)
  • Block 3 captures another numeric value (say r)
  • Within Block 3, on a new page, I want to display the embedded variable embedVar = r/p

For this I am running the following js code:

	var p ="${q://QID7/ChoiceTextEntryValue}" ;
var r = "${q://QID2/ChoiceTextEntryValue}" ;
var result = Math.ceil(r/p) ;
Qualtrics.SurveyEngine.setEmbeddedData( 'embedVar', result );

This just does not work. I have tried to put it on “onload”, “onunload”, “onsubmit”, “onready” (please don’t mind the case-insensitivity). I have put this code in the page where I record the response r, the page where I am supposed display the updated embedVar. I exhausted every combination. It just remains -99 or it shows a 0.

 

What does work is if I equate embedVar to p, it does show the actually recorded p. This makes me suspect that the js is not running at the appropriate time. Can anyone please help? It is a very simple code that is supposed to work, but it just doesn’t to work.

 

To note: The block 3 runs a loop where, in every loop, the value p is recorded and I try to update the embedVar and try to display it. 

@sharadhotha You should run the code when there already value in QID2 (e.g when they click next button) and get the value in the text entry box, not the result (it haven’t populated yet). The code should run before you load “embedVar” so put this code in the page where you record r

Qualtrics.SurveyEngine.addOnReady(function()
{
var nextButton = document.getElementById('NextButton');
nextButton.addEventListener('click', function() {
var p = "${q://QID7/ChoiceTextEntryValue}" ;
var r = document.getElementById('QR~QID2').value;

var result = Math.ceil(r/p);
Qualtrics.SurveyEngine.setEmbeddedData( 'embedVar', result );
})
});

Hope this helps


Forum|alt.badge.img+1
  • Author
  • 1 reply
  • October 25, 2023

I have been trying to run a very simple js code on Qualtrics for about 5 hours now.

 

There are 3 blocks:

  • Block 1 captures a numeric value (say p)
  • Block 2 creates an embedded value (say embedVar =-99)
  • Block 3 captures another numeric value (say r)
  • Within Block 3, on a new page, I want to display the embedded variable embedVar = r/p

For this I am running the following js code:

	var p ="${q://QID7/ChoiceTextEntryValue}" ;
var r = "${q://QID2/ChoiceTextEntryValue}" ;
var result = Math.ceil(r/p) ;
Qualtrics.SurveyEngine.setEmbeddedData( 'embedVar', result );

This just does not work. I have tried to put it on “onload”, “onunload”, “onsubmit”, “onready” (please don’t mind the case-insensitivity). I have put this code in the page where I record the response r, the page where I am supposed display the updated embedVar. I exhausted every combination. It just remains -99 or it shows a 0.

 

What does work is if I equate embedVar to p, it does show the actually recorded p. This makes me suspect that the js is not running at the appropriate time. Can anyone please help? It is a very simple code that is supposed to work, but it just doesn’t to work.

 

To note: The block 3 runs a loop where, in every loop, the value p is recorded and I try to update the embedVar and try to display it. 

@sharadhotha You should run the code when there already value in QID2 (e.g when they click next button) and get the value in the text entry box, not the result (it haven’t populated yet). The code should run before you load “embedVar” so put this code in the page where you record r

Qualtrics.SurveyEngine.addOnReady(function()
{
var nextButton = document.getElementById('NextButton');
nextButton.addEventListener('click', function() {
var p = "${q://QID7/ChoiceTextEntryValue}" ;
var r = document.getElementById('QR~QID2').value;

var result = Math.ceil(r/p);
Qualtrics.SurveyEngine.setEmbeddedData( 'embedVar', result );
})
});

Hope this helps

Thanks a lot for the quick reply!

 

I just tried this, and it still shows -99. Does this have anything to do with me trying to display this within the block? Or the fact that I am looping the question to get the response r?


Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+29
  • QPN Level 8 ●●●●●●●●
  • 1096 replies
  • October 25, 2023

Thanks a lot for the quick reply!

 

I just tried this, and it still shows -99. Does this have anything to do with me trying to display this within the block? Or the fact that I am looping the question to get the response r?

@sharadhotha In that case, your id will be in the form “QR~n_QID2” with n being the loop that you’re in. So if you have to change the code to this

Qualtrics.SurveyEngine.addOnReady(function()
{
var QuesID = this.questionId;
var nextButton = document.getElementById('NextButton');
nextButton.addEventListener('click', function() {
var p = "${q://QID151/ChoiceTextEntryValue}" ;
var r = document.getElementById('QR~'+QuesID).value;

var result = Math.ceil(r/p);
Qualtrics.SurveyEngine.setEmbeddedData( 'embedVar', result );
})
});

Let me know if it work