Using .getChoiceAnswerValue(); for a question other than 'this'

Pete_LPete_L Community Member Qubie ✭

Using .getChoiceAnswerValue(); for a question other than 'this'

Hi everyone,

If you have 3 questions on the same screen, how do you use .getChoiceAnswerValue(); to capture the values from the other questions? I cannot seem to get it to work! Here is an example to clarify what I mean:

DESC [descriptive item]
How do you feel about the following items?

Q2
Dogs
1: Good
2: Bad

Q3
Cats
1: Good
2: Bad

I have javascript on the DESC variable to try capturing the values selected at Q2 and Q3 OnPageSubmit, but this does not seem to work correctly.

Qualtrics.SurveyEngine.addOnPageSubmit(function() 
{
    var q2= document.getElementById('QID2').getChoiceAnswerValue();
    var q3= document.getElementById('QID3').getChoiceAnswerValue();
    console.log(q2);
    console.log(q3);
});

This throws an error saying .getChoiceAnswerValue is not a function.

Tagged:

Answers

  • ShashiShashi IndiaCommunity Member Wizard ✭✭✭✭✭

    Hello @Pete_L ,

    You need to paste below code at each question JS

    Qualtrics.SurveyEngine.addOnPageSubmit(function() 
    {
        var q2= this.getChoiceAnswerValue();
    
        console.log(q2);
    });
    
  • Pete_LPete_L Community Member Qubie ✭

    If I do it like this, will those variables be available outside of the questions that executed the logic?

    I would have to create a hidden Qualtrics question at the bottom of the screen to run JS after all the other questions have executed I think. But it would only work if the q1, q2, q3 defined by the above questions will be available to other elements on the screen.

  • w.patrick.galew.patrick.gale Community Member Guru ✭✭

    @Pete_L You do not need to use getChoiceAnswerValue UNLESS you are trying to pull the values from questions on the same page. For subsequent page javascript, where you wish to pull previously entered values, simply use piped text. Any of the piped text you can add through the question editor will work in your javascript. So if you have another page after the Q2 and Q3 questions you can test it by adding piped text to the question description like so (of course this assumes your Q2 and Q3 questions have an ID of QID2 and QID3, and they are using the multiple choice question type):

    Response to Q2 is ${q://QID2/ChoiceGroup/SelectedChoices} 
    Response to Q3 is ${q://QID3/ChoiceGroup/SelectedChoices}
    

    In your Javascript you can test it like so:

    $myQ2Val = "${q://QID2/ChoiceGroup/SelectedChoices}";
    alert($myQ2Val);
    

    It sounds like you want to pull the values later in the survey so this may be the path of least resistance. (I personally run the Tools/Auto-Number Questions/Internal numbering option to always set the survey question numbering to match that of the piped text fields for easier referencing in the survey editor).

  • Pete_LPete_L Community Member Qubie ✭
    edited April 24

    I have to compute the value on the same page before moving to the next page sadly. The text changes on the very next page based on how many of the items on this page are answered. :(

    EDIT to clarify:

    If you select good one item say "this item", if you say good to two items, "these two items", if you say good to more than two, "all of the items" kind of thing.

    I want the text to be available for screen readers, so I'm concerned about doing this on the following page OnLoad vs. computing it while exiting the page. Does that make sense?

  • w.patrick.galew.patrick.gale Community Member Guru ✭✭

    @Pete_L On the next page are you using piped text or display logic to show your answer? Or I guess I should say, how are you saving your answer on the first page to use on the second page?

  • Pete_LPete_L Community Member Qubie ✭

    I was hoping to have javascript that could 'count' the number of responses on the page OnSubmit, and then set an embedded data text fill with the appropriate text. My thinking was to put all of the Javascript on the 'DESC' variable in my example above.

    OnSubmit it would check the selected response of all the other variables on the screen and count them. If count=0, if count=1, if count=2, and so on, then set the embeddedData appropriately.

    Then I would have the follow-up page just pipe in the embedded data text fill as appropriate.

    I have not been able to set Embedded Data values with an event listener using onkeyup or onclick functions either. Seems to only work OnReady, OnLoad, OnUnload, or OnSubmit.

  • w.patrick.galew.patrick.gale Community Member Guru ✭✭
    edited April 24

    Pete_L So embedded data does not get set until the page is submitted so you will not have it until the next page. Here is something you can play with:

    // when changes are made to selections
    jQuery( "select[id='QR\~QID2']" ).change(function() {checkValues();});
    jQuery( "select[id='QR\~QID3']" ).change(function() {checkValues();});
    
    // get the current values
    function checkValues(){
            var strQ2 = jQuery( "select[id='QR\~QID2']" ).val();
            var strQ3 = jQuery( "select[id='QR\~QID3']" ).val();
    }
    

    It needs to be noted that this will work with dropdown lists but not radio button lists. Radio button lists are impossible to work with in Qualtrics using javascript. Save yourself some headaches and use dropdown boxes if you are working with javascript.

  • Pete_LPete_L Community Member Qubie ✭

    I'll give this a shot and see how it plays out. Will reply later today with how it goes!

  • w.patrick.galew.patrick.gale Community Member Guru ✭✭
    edited April 24

    @Pete_L I added a caveat to the code above (use dropdown selections) and changed it slightly because I believe it was unworkable earlier.

Sign In to Comment