Javascript to get selected choices and creating an embedded data | XM Community
Skip to main content

Hi. No matter what I do I don’t get it right, even with the help of GPT… 

I want to use the ${q://QID37/ChoiceGroup/SelectedChoices} ( it’s a matrix question/crousel). 

I want to do the following:

if (scores.every(score => score >= 1 && score <= 3)) { trustScore = "Low";

if (scores.every(score => score >= 6 && score <= 7)) { trustScore = "High";

if it is neither only 6 or 7 in the choices or only 1 or 2 or 3 in the choices, then call trustScore = “Normal”. 

Qualtrics.SurveyEngine.setEmbeddedData('Trust_Score', trustScore);

 

I need it of course as a hidden question… 

any idea how I get it done? I don’t understand why anything I do, return Normal or not return at all… :

 

I guess it starts with: 

Qualtrics.SurveyEngine.addOnReady(function () { var $this = jQuery(this.questionContainer); jQuery(".QuestionText", $this).hide();

Then I’m missing the part of Get… what should it be… Qualtrics.SurveyEngine.get??

 

and it should end: 

// Set the result Qualtrics.SurveyEngine.setEmbeddedData('Trust_Score', trustScore); // Move to next question jQuery("#NextButton").click(); });

 

 

@TomG @Nam Nguyen  - Your help here would be amazing!

 

 

Thank you!

 

 

 

 

@deveer2024 
What are you trying to do here? Without context I can’t help through these few line of code. Also, the ChoiceGroup/SelectedChoices is for MC question, not MatrixTable. Pls take a picture of your question and describe what you need


@Nam Nguyen Thank you so much for the response and you are absolutly correct. I need to be more clear so I will be now methodic :)

I’m sharing here a test file (.qsf file) with the question survey and the JS question (as in the image) and what I’m trying to do will be clear from the survey file and here in my elaboration:

In general after the Matrix question (in my original survey it is QID37) I want the JS to analyze the selected choices of the response and determine one of the following: either the trust score is High, Normal or Low by the following rule: 

 

High - all choices were 6 or 7 in any combination (for example 6.7.7.7 or 7,6,6,7 etc)

Low - all choices were 1 or 2 or 3 in any combination (for example 1111 or 1231 or 2321 etc)

Normal - everything that doesn’t fall into High or Low (so any combination of the entire scale that doesn’t meet the High Low criteria). 

 

After it analyzes the scores and make the decision - create an embedded data that is called: Trust_Score and apply High, Normal, Low as a value. Store it as part of the survey embedded data.

 

No matter what I tried it didn’t work, but again, it maybe I didn’t even know what to start with :)

 

Thanks!!

 


@deveer2024 Your matrix is in carousel format which make it not easy to code and you just want to save the Embedded Data so coding is unessesary here.
Just make a feel branches in your surveyflow like this Default set it to Normal then 2 branches for High and Low


 


@Nam Nguyen  You are the best!!!!!!!!!!!!!!!!!! it works!!! Finally!! I can truly just say you are a life saver!!


@deveer2024 Glad to help 👍


Leave a Reply