Search code examples
javascriptqualtrics

Getting the column choice values from a matrix table using Qualtrics API


I have a matrix with two columns. I want to get the inputs of both columns on page submit. I have been using getChoiceValue, and I have no problem getting the value of the right column. How can I get the value of the left column?

Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
// this does not work
  let left_column_choice = this.getChoiceValue(0);
// this works for sure
  let right_column_choice = this.getChoiceValue(1); 
});

Solution

  • I don't know anything about 'Qualtrics' or any functions it contains, but if you are looking for a plain vanilla JS solution, this might help.

    function getColumnInfo (cols,c) {
      let arr = [],
          sel = document.querySelectorAll('.tbl td');
      for( const [ndx,val] of sel.entries(sel) ) {
        if ((ndx % cols) == c) arr.push(val.textContent) 
      };
      return arr;
    }
    let info = getColumnInfo(4,1);  // of 4 column table, get column 1
    alert('Column #2 contents:\n'+info);
    <table border='1' class='tbl'>
    <caption> Table </caption>
    
     <tr>
      <td>Group 1 </td>
      <td>A </td>
      <td>a </td>
      <td>1 </td>
     </tr>
     <tr>
      <td>Group 2 </td>
      <td>B </td>
      <td>b </td>
      <td>2 </td>
     </tr>
     <tr>
      <td>Group 3 </td>
      <td>C </td>
      <td>c </td>
      <td>3 </td>
     </tr>
    </table>