Get selected answer from dropdown list JS | XM Community
Skip to main content
Solved

Get selected answer from dropdown list JS


Forum|alt.badge.img+1
I have a drop down list of possible values for one of my questions that asks for the respondents CRM vendor. I would like to get the text value of the selected answer from the dropdown list and set it to an embedded variable. My attempt looks like the below: Qualtrics.SurveyEngine.addOnPageSubmit(function () { var CRM = this.getSelectedAnswers(); Qualtrics.SurveyEngine.setEmbeddedData('CRM' ,CRM); }; Of course, my code doesn't work and I assuming either my syntax or parameters (maybe both?) are wrong. Any help is highly appreciated.

Best answer by TomG

> @HPowell said: > Thanks Tom. Instead of return the number of the choice, is there anyway to have it return the text? i.e. if Salesforce was choice 1 it would assign CRM variable the string of Salesforce and not the value of 1. ``` Qualtrics.SurveyEngine.addOnPageSubmit(function() { Qualtrics.SurveyEngine.setEmbeddedData("CRM",jQuery("#"+this.questionId+" option:selected").text()); }); ```
View original

6 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5926 replies
  • April 8, 2019
Try getting selected choices instead of answers: ``` Qualtrics.SurveyEngine.addOnPageSubmit(function () { var CRM = this.getSelectedChoices(); Qualtrics.SurveyEngine.setEmbeddedData('CRM' ,CRM); }; ```

Forum|alt.badge.img+1
  • Author
  • 10 replies
  • April 8, 2019
Thanks Tom. Instead of return the number of the choice, is there anyway to have it return the text? i.e. if Salesforce was choice 1 it would assign CRM variable the string of Salesforce and not the value of 1.

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5926 replies
  • Answer
  • April 8, 2019
> @HPowell said: > Thanks Tom. Instead of return the number of the choice, is there anyway to have it return the text? i.e. if Salesforce was choice 1 it would assign CRM variable the string of Salesforce and not the value of 1. ``` Qualtrics.SurveyEngine.addOnPageSubmit(function() { Qualtrics.SurveyEngine.setEmbeddedData("CRM",jQuery("#"+this.questionId+" option:selected").text()); }); ```

  • 3 replies
  • May 14, 2020

Get selected answer from dropdown list JSHi I have tried it but It show us nothing in console. How can I see it works true?
Well I have run it before page not submited:
Qualtrics.SurveyEngine.addOnPageSubmit(function () { 
var SelectedChoices = jQuery("#"+this.questionId+" option:selected").text() 
console.log(SelectedChoices); });
image.pngThen, I submit page and I wait It show us selected answer in previous submited page. But It shows me nothing.
image.pngI guess it doesnt find this.questionId. How can I fix it?
Thanks.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5926 replies
  • May 14, 2020

@firat,
The problem isn't with this.questionId. The JS is to get the text from a dropdown, and your question isn't a dropdown.


Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2028 replies
  • October 20, 2020

I've been using the following to extract the text from the webpage itself, maybe it'll work for you as well.

Qualtrics.SurveyEngine.addOnPageSubmit(function (type) {
    if (type == "next") {
        var selChoice = this.getSelectedChoices();
        var choiceRef = "";
        var choiceText = "";
        for (let i = 0; i < selChoice.length; i++) {
            choiceRef = "#" + this.questionId + "-" + selChoice[i] + "-label > span";
            choiceText = document.querySelector(choiceRef).innerHTML;
            Qualtrics.SurveyEngine.setEmbeddedData(choiceText,true);
        }
    }
});


Leave a Reply