javascript matrix table get choice value as number | XM Community
Skip to main content
Hi, I'd like to conditionally assign participants to 1 of 8 items based on how familiar they are with each of the 8 items.



In my Qualtrics survey, participants first rate all 8 items using a Matrix Table question. I'd like to use those ratings to conditionally assign participants to 1 item (i.e., if else statements).



Because I want to make a list of items that satisfy my "familiar" or "unfamiliar" definitions, I have to use JavaScript.



I've added my JavaScript code to a question that comes after the rating Matrix Table question (QID22) so that I can refer to recoded answers from that matrix question.



Below, I've included only a small part of my JavaScript code because this code doesn't work like I expect it to.



The possible recoded values range from 0-3 by units of 1 (i.e., 0, 1, 2, 3). When I preview my survey, my embedded data don't have any values from 0 to 3. When convert SelectedAnswerRecode values to numbers with the Number function (e.g., Number("${q://QID22/SelectedAnswerRecode/1}")), my embedded data have only 0 values. I've also tried parseInt() and parseFloat() without luck (both give seemingly "blank" embedded data values).



I'm not sure what to do from here. Does anyone have any ideas? Thank you!



Qualtrics.SurveyEngine.addOnload(function() {

/*Place your JavaScript here to run when the page loads*/

});



Qualtrics.SurveyEngine.addOnReady(function() {



// save every answer choice into an object

var item1 = "${q://QID22/SelectedAnswerRecode/1}";

var item2 = "${q://QID22/SelectedAnswerRecode/2}";

var item3 = "${q://QID22/SelectedAnswerRecode/3}";

var item4 = "${q://QID22/SelectedAnswerRecode/4}";

var item5 = "${q://QID22/SelectedAnswerRecode/5}";

var item6 = "${q://QID22/SelectedAnswerRecode/6}";

var item7 = "${q://QID22/SelectedAnswerRecode/7}";

var item8 = "${q://QID22/SelectedAnswerRecode/8}";



// save all values as embeded data

Qualtrics.SurveyEngine.setEmbeddedData("item1", (item1));

Qualtrics.SurveyEngine.setEmbeddedData("item2", (item2));

Qualtrics.SurveyEngine.setEmbeddedData("item3", (item3));

Qualtrics.SurveyEngine.setEmbeddedData("item4", (item4));

Qualtrics.SurveyEngine.setEmbeddedData("item5", (item5));

Qualtrics.SurveyEngine.setEmbeddedData("item6", (item6));

Qualtrics.SurveyEngine.setEmbeddedData("item7", (item7));

Qualtrics.SurveyEngine.setEmbeddedData("item8", (item8));

// if () and if else () logic follows

});



Qualtrics.SurveyEngine.addOnUnload(function() {

/*Place your JavaScript here to run when the page is fully displayed*/

});
Hi @nickmm



Your code at least the part doesn't seem to have any faults.

I even tried the same code for 1 item in my own survey and it works just fine.

Some tips to check code:

1. Try refreshing account

2. Add `alert(); ` function after points you think the survey is not working.

3. Check Question ID if this is the relevant question from which the text is being piped.

4. Try logging in and out of your account.



Regards

NiC
Thank you, NiC!



1. I've tried refreshing my account without any luck.

2. How can I use `alert()` to help?

3. Question ID is correct (good call).

4. I logged in and out of my account without any luck.



I'm going to keep this question open but you have been very helpful so far!
First of all you can use the alert function to see if the data is being pulled.

into the variables defined for eg:



var item1 = "${q://QID22/SelectedAnswerRecode/1}";

alert(item1) ;



now if you run the survey , the expected recode value should be shown on the alert message as soon as you click next and the next question loads.



You can add multiple alerts() like

` alert("item1: "+item1);`



So essentially we will be using alert function as flags indicating the output till where the program has ran and helping us judge what is working and what is not.
Hi NiC -- `alert()` is a cool trick, thank you!



I tried a more "hacky" approach. I added a Qualtrics Text/Graphic item with piped text for the recoded answers to each Matrix Table question as well as the embedded data I presumably assigned in my JavaScript"



${q://QID22/SelectedAnswerRecode/1}

${q://QID22/SelectedAnswerRecode/2}

${q://QID22/SelectedAnswerRecode/3}

${q://QID22/SelectedAnswerRecode/4}

${q://QID22/SelectedAnswerRecode/5}

${q://QID22/SelectedAnswerRecode/6}

${q://QID22/SelectedAnswerRecode/7}

${q://QID22/SelectedAnswerRecode/8}



item1 = ${e://Field/uptownfunk}

item2 = ${e://Field/smellslike}

embedded1 = ${e://Field/treatment}

​embedded2 = ${e://Field/song}



When I publish and preview my survey, and then choose "3" for each Matrix Table item, this Qualtrics question displays this:



3

3

3

3

3

3

3

3



item1 =

item2 =

embedded1 = "Custom treatment text"

embedded2 = "Custom treatment text"



"Custom treatment text" is almost expected behavior. The JavaScript created new embedded data as text".



But if item1 and item2 are blank---to my knowledge, they shouldn't be because I used "SelectedAnswerRecode/1" and "SelectedAnswerRecode/2" and Qualtrics.SurveyEngine.setEmbeddedData("item1", (item1)) and Qualtrics.SurveyEngine.setEmbeddedData("item2", (item2)) in my code in my original post--then "Custom treatment text" can't possibly be produced by the logical statements I use in the rest of my JavaScript.



I'm at a loss :neutral: . This problem probably seems hopeless and confusing. Thank you for all your help!
Hi @nickmm,

are all your embedded defined in your survey flow?

In your original code you don't modify embedded fields called "uptownfunk" or "smellslike". Their names are "item1" and "item2".

Therefore you should use:

item1 = ${e://Field/item1}

item2 = ${e://Field/item2}

Leave a Reply