How to reference to a selected choice on the current page that hasn't been submitted yet? | XM Community
Solved

How to reference to a selected choice on the current page that hasn't been submitted yet?

  • 30 October 2018
  • 4 replies
  • 115 views

Badge
Dear All

I am looking to take a respondent's currently selected (but not yet submitted) choice, perform a mathematical operation on it and then display it to the respondent on the same page as the question, so they can see the result before they submit their answers.

The code I am currently using (placed in the HTML view section of a subsequent question that is on the same page) (note: I don't actually attempt to perform the mathematical operation yet as the choice is not being pulled properly - this is merely meant to display their currently selected choice at the moment);

Code
<p id="test"></p>

<script>
var myVar = setInterval(myFunction, 2000);
function myFunction() {
var y = parseInt(${q://QID21/ChoiceNumericEntryValue/1});
document.getElementById("test").innerHTML="The value of y is " + y + ".";
}
</script>

Output

*After 2 seconds, "The value of y is NaN." appears on the screen and doesn't change if the value of q://QID21/ChoiceNumericEntryValue/1 is changed (but not submitted).

If I understood correctly, it is taking the value of q://QID21/ChoiceNumericEntryValue/1 when the page first loads and this is why it is being returned as undefined.

How can I instead make it refer to the currently selected choice, even if it has not yet been submitted? I was previously advised that using setInterval would be able to achieve this but am unsure on how to refer to the variable in a way that will cause it to update as the respondent updates their choice, instead of just continuously grabbing the (undefined) value from when the page first loaded.

Any help on this matter would be greatly appreciated.

The question is a constant sum question and q://QID21/ChoiceNumericEntryValue/1 is one of 10 inputs the respondent makes, if that makes any difference.
icon

Best answer by AdeGendre 31 October 2018, 01:50

View original

4 replies

Hello @ChloeM ,

Use below code to fetch correct value and paste it in js(onReady) of question

var myVar = setInterval(myFunction, 2000);
function myFunction() {
var y = parseInt(jQuery("[id='QR~QID21~1']").val());
document.getElementById("test").innerHTML="The value of y is " + y + ".";
}
Badge
Hello @Shashi

Many thanks for the response, it is much appreciated! Unfortunately, when I do this I get as the output "The value of y is undefined.", which does feel like progress but still isn't quite there.

[For anyone else looking at this thread, this is the same whether you include the parseInt() function or not].
I have done something similar, but with a Slider question. Here is the code:

Qualtrics.SurveyEngine.addOnload(function() {
/*Place your JavaScript here to run when the page loads*/
var result=0;

this.questionclick = function(event,element){
result = this.getChoiceValue(1);
document.getElementById("result").innerHTML= result;
}

});

Then in the HTML View of the question, type the following:

The value is currently: <span id="result">0</span>
Badge
@AdeGendre

Thank you very much, that has got it!

Thanks to both of you for the help, it is very much appreciated!

Leave a Reply