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

ChloeMChloeM Belfast, Northern IrelandCommunity Member Qubie ✭

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

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

<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.

Best Answer

  • AdeGendreAdeGendre Melbourne Qubie ✭
    Accepted Answer

    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>
    

Answers

  • ShashiShashi IndiaCommunity Member Wizard ✭✭✭✭✭
    edited October 2018

    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 + ".";
        }
    
  • ChloeMChloeM Belfast, Northern IrelandCommunity Member Qubie ✭
    edited October 2018

    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].

  • ChloeMChloeM Belfast, Northern IrelandCommunity Member Qubie ✭

    @AdeGendre

    Thank you very much, that has got it!

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

Sign In to Comment