I want to retrieve the answer choice value from a question | XM Community
Skip to main content

I am new on the plartform and I want to retrieve the answer choice value(actual text) of an option selected in a multiple choice question where you can only select a single choice, but all I get with my code is an empty string.

here is my code:


Qualtrics.SurveyEngine.addOnPageSubmit(function() {
    var questionID = "QID278"; // Replace with your question ID
    var question = Qualtrics.SurveyEngine.getInstance(questionID);

    
      

        console.log("Selected choice text: " +question.getChoiceAnswerValue());
   
   
});

I also tried using question.getSelectedAnswerValue () but it gives me Null.


any advise will be appreciated.

Hi @Kabo_VB 
Use this code instead, it will give you the selected choice text from QID278

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var questionID = "QID278";
var question = Qualtrics.SurveyEngine.getInstance(questionID);

var selectedChoiceIndex = question.getSelectedChoices())0];

if (selectedChoiceIndex !== null && selectedChoiceIndex !== undefined) {

var selectedLabel = document.querySelector('#QID278-' + selectedChoiceIndex + '-label span');
var selectedChoiceText = selectedLabel ? selectedLabel.textContent : '';

console.log("Selected choice text: " + selectedChoiceText);
} else {
console.log("No choice selected");
}
});

 


Leave a Reply