getAnswerCode()

The getAnswerCode() function (client-side and server-side) retrieves the answer code for the first response selected in a question, or the code of an answer selected in a specific position. The code matches the value in the Code field on the answer record.

Syntax

Use this syntax for the getAnswerCode() function:

            getAnswerCode('QUESTION_CODE', answer_index); 

          

Return Value

The getAnswerCode() function returns the answer code as a string.

            'ANSWER_CODE' 

          

Parameters

Note:

The question code is required.

The getAnswerCode() function accepts the following parameters:

  • QUESTION_CODE (string) - Specifies the code of the question for which you want to retrieve the selected answer code. You can find it in the Code field on the question record.

  • answer_index (number) - For questions with multiple answers, this parameter specifies the index of the answer to retrieve. The index starts at 1. If omitted, the function returns the code for the first selected answer.

Examples

The following examples show how to use the getAnswerCode() function.

Retrieving the First Selected Answer Code for a Question

This example retrieves the code for the first selected answer in the "Select color" question with the code COLOR.

              const answerCode = getAnswerCode('COLOR');
console.log(answerCode); 

            

Suppose the question has the following answers:

  1. Blue (code BLUE)

  2. Beige (code BEIGE)

  3. Gray (code GRAY)

If a user selects "Gray", the code in this example will return GRAY.

Retrieving a Specific Answer Code for a Question

The example retrieves the code for the second selected answer in the "Select color" question with the code COLOR.

              const answerCode = getAnswerCode('COLOR', 2);
console.log(answerCode); 

            

Suppose the question has three answers:

  1. Blue (code BLUE)

  2. Beige (code BEIGE)

  3. Gray (code GRAY)

If a user selects "Blue" and "Gray", the code in this example will return GRAY, which is the code for the second selected answer.

Related Topics

General Notices