Javascript: Default choices on multiple (!) dropdown answers in Matrix Table | XM Community
Solved

Javascript: Default choices on multiple (!) dropdown answers in Matrix Table

  • 31 May 2018
  • 3 replies
  • 168 views

Userlevel 1
Badge +6
Hi all,

I am trying to select default answers based on embedded data. The problem I have is two-fold:

First, and most importantly: I cannot figure out how to select different values for several different drop-down options in the same Matrix Table.
I found the following online, which lets me select the SAME answer on all dropdowns in the same question (in this example, the second from the bottom):
if($('QID17')) {$$('#QID17 select').each(function(s){ s.value = (s[s.length-2].value); }) }
But how do I adapt this to select different answers for the different drop-downs?

Second, my current JavaScript is long, using "IF embeddeddataX = 1 THEN set answer to 1, IF embeddeddataX = 2 THEN set answer to 2, ..."
I am looking for a more elegant way to use the numeric value from embedded data to set the answer choice.

Any help is appreciated!
icon

Best answer by Rich_Boits_Walker 2 June 2018, 05:55

View original

3 replies

Userlevel 5
Badge +7
I've used the setChoiceValueByRecodeValue method documented in the link below. The documentation is describing it's use for a single select, but it can be tweaked to work with a matrix table by passing row in. This works for all four answer types in the matrix table (single, multiple, dropdown, and drag & drop).

`setChoiceValueByRecodeValue(row,value,'true')`

https://s.qualtrics.com/WRAPI/QuestionAPI/classes/Qualtrics%20JavaScript%20Question%20API.html#method_setChoiceValueByRecodeValue
Userlevel 5
Badge +7
Here is a simple example of what I mean using embedded data fields statement1 and statement2 that are set before the matrix question block.

Qualtrics.SurveyEngine.addOnload(function()
{
var selectedPunch1 = parseInt("${e://Field/statement1}");
var selectedPunch2 = parseInt("${e://Field/statement2}");

// matrix table arguements are (row,value,'true')
// selects punch in a matrix table (row_#_in_matrix_table,punch_from_var,'true')
this.setChoiceValueByRecodeValue(1,selectedPunch1,'true');
this.setChoiceValueByRecodeValue(2,selectedPunch2,'true');
});

Note that I added parseInt around the embedded data to workaround the piped text call's double slash which is a comment in javascript.
Userlevel 1
Badge +6
This is worked great for me - thanks so much for sharing this!

Leave a Reply