How to get field ids for answers on matrix questions?

I would like to be able to pull just the option that is selected from a matrix question. I would then like to include that id as variable in the email integration.

I checked the matrix field docs and was unable to figure out how to implement this.

Hi Grey,

Here’s how you can get the answer of your matrix. Create a hidden field to hold the answer of the matrix. For this example, we’ll name the field “matrixAnswer”

Then create a rule, the trigger will be on field value change, then select your matrix.

On your advanced logic, Add this code


const matrixPair = document.querySelectorAll('input[name^="yourMatrixId"]');

matrixPair.forEach(input => {
  const questionId = input.getAttribute('data-question-id');
  matrixAnswer.value = yourMatrixId.value[questionId][0];
});

Don’t forget to update yourMatrixId to your matrixId.

Let me know if it works!

It gets me the first answer. Is there a way to get all answers from the matrix? Would it require a hidden field for each row in the matrix?

The 0 is an index, and it refers to the order of the questions in your matrix.

For example, in the previous code snippet:

matrixAnswer.value = yourMatrixId.value[questionId][0];

This retrieves the first answer from the list of answers in your matrix.

You can either save the answers as an array and assign them to your hidden value or create a separate hidden value for each answer.

For an array, here’s how you can do it:

const matrixPair = document.querySelectorAll('input[name^="yourMatrixId"]');
let answer = [];

matrixPair.forEach((input, index) => {
  const questionId = input.getAttribute('data-question-id');
  answer.push(yourMatrixId.value[questionId][index]);
});

matrixAnswer.value = answer;

I should be able to figure it out from here. Thank you for the help and quick responses.

Hey Kenny,

When I use the array method, I am still only able to get the very first answer. I would like to save each answer as an individual hidden value.

const matrixPair = document.querySelectorAll('input[name^="behavioral_questions"]');
let answer = [];

matrixPair.forEach((input, index) => {
  const questionId = input.getAttribute('data-question-id');
  answer.push(behavioral_questions.value[questionId][index]);
});

THAmatrixAnswer1.value = answer[0];
THAmatrixAnswer2.value = answer[1];
THAmatrixAnswer3.value = answer[2];
THAmatrixAnswer4.value = answer[3];
THAmatrixAnswer5.value = answer[4];
THAmatrixAnswer6.value = answer[5];
THAmatrixAnswer7.value = answer[6];
THAmatrixAnswer8.value = answer[7];
THAmatrixAnswer9.value = answer[8];
THAmatrixAnswer10.value = answer[9];
THAmatrixAnswer11.value = answer[10];
THAmatrixAnswer12.value = answer[11];
THAmatrixAnswer13.value = answer[12];
THAmatrixAnswer14.value = answer[13];
THAmatrixAnswer15.value = answer[14];
THAmatrixAnswer16.value = answer[15];
THAmatrixAnswer17.value = answer[16];
THAmatrixAnswer18.value = answer[17];
THAmatrixAnswer19.value = answer[18];
THAmatrixAnswer20.value = answer[19];
THAmatrixAnswer21.value = answer[20];
THAmatrixAnswer22.value = answer[21];
THAmatrixAnswer23.value = answer[22];
THAmatrixAnswer24.value = answer[23];
THAmatrixAnswer25.value = answer[24];

Here is what I have been trying.

Hi Grey,

You can use this approach instead:


const targetObject = { ...behavioral_questions.value }; 
const answers = Object.values(targetObject).flat();

The answers variable will contain all the selected answers for the question in the matrix.

Let me know if you have any questions!