πŸ”˜Single choice

Configure a single choice (radios)

Introduction

  1. Script a single choice question

Basic scripting of a single choice question
  1. Open the Javascript editor

  2. Copy the Initial template to enable the Survey Library

  3. Enable the configuration for Single Choice by importing its Survey Library URL

// Import the libraries
let scripts = [
  "https://survey-library.azurewebsites.net/single_choice.js"
];

let promises = scripts.map((script) => {
  return new Promise((resolve, reject) => {
    document.querySelector("body").style.opacity = "0";
    let s = document.createElement("script");
    s.src = script;
    s.onload = resolve;
    s.onerror = reject;
    document.head.appendChild(s);
  });
});

Promise.all(promises)
  .then(() => {
    /* All scripts are loaded, you can execute your functions here */
    

    /* All script ends here */
    document.querySelector("body").style.opacity = "1";
  })
  /* If a library could not be imported throw an error */
  .catch((error) => {
    console.error(`Failed to load script: ${error}`);
  });

Examples

Randomize a subset of answer options

We have a question code called Q1 that has 7 answer options where codes are 1-5, 99 and 999.

Let's say that we want to randomize from 1 to 5, and anchor codes 99 and 999.

Definining a group of answer options

Unlisted answers codes will be treat as anchores, meaning that they will remain always in the same position.

// Insert the Library URLs here
let scripts = [
  "https://survey-library.azurewebsites.net/single_choice.js"
];

let promises = scripts.map((script) => {
  return new Promise((resolve, reject) => {
    document.querySelector("body").style.opacity = "0";
    let s = document.createElement("script");
    s.src = script;
    s.onload = resolve;
    s.onerror = reject;
    document.head.appendChild(s);
  });
});

Promise.all(promises)
  .then(() => {
    /* All scripts are loaded, you can execute your functions here */
    single_choice({
      question_code : "Q1", // Question code to configure
      randomize : {         // Settings object for randomize
        answer_groups : [   // List of answer groups to randomize
          [1,2,3,4,5]
        ],
        randomize_groups : false // Do not randomize groups
      }
    });

    /* All script ends here */
    document.querySelector("body").style.opacity = "1";
  })
  /* If a library could not be imported throw an error */
  .catch((error) => {
    console.error(`Failed to load script: ${error}`);
  });

Randomize more than one subset of answer options

We have a question called Q1 that has 7 answer options where codes are 1-5, 99 and 999.

Let's say that we want to group answers and randomize elements inside the groups.

Defining multiple subsets of answer options

In this example, we are defining two groups with answer codes:

  • Group 1: 1,2,3,4

  • Group 2: 5,99

  • Anchors: 999

Unlisted answers codes in groups will be treat as anchors, meaning that they will remain always in the same position.

// Insert the Library URLs here
let scripts = [
  "https://survey-library.azurewebsites.net/single_choice.js"
];

let promises = scripts.map((script) => {
  return new Promise((resolve, reject) => {
    document.querySelector("body").style.opacity = "0";
    let s = document.createElement("script");
    s.src = script;
    s.onload = resolve;
    s.onerror = reject;
    document.head.appendChild(s);
  });
});

Promise.all(promises)
  .then(() => {
    /* All scripts are loaded, you can execute your functions here */
    single_choice({
      question_code : "Q1", // Question code to configure
      randomize : {         // Settings object for randomize
        answer_groups : [   // List of answer groups to randomize
          [1,2,3,4],        // Group 1
          [5,99]            // Group 2
        ],
        randomize_groups : false // Do not randomize groups
      }
    });

    /* All script ends here */
    document.querySelector("body").style.opacity = "1";
  })
  /* If a library could not be imported throw an error */
  .catch((error) => {
    console.error(`Failed to load script: ${error}`);
  });

Randomize subsets of answers options

We have a question called Q1 that has 7 answer options where codes are 1-5, 99 and 999.

Let's say that we want to randomize three different groups and anchor options defined as follows:

  • Group 1: 1,2

  • Group 2: 3,4

  • Group 3: 5

  • Anchors: 99,999

Unlisted answers codes in groups will be treat as anchors, meaning that they will remain always in the same position.

// Insert the Library URLs here
let scripts = [
  "https://survey-library.azurewebsites.net/single_choice.js"
];

let promises = scripts.map((script) => {
  return new Promise((resolve, reject) => {
    document.querySelector("body").style.opacity = "0";
    let s = document.createElement("script");
    s.src = script;
    s.onload = resolve;
    s.onerror = reject;
    document.head.appendChild(s);
  });
});

Promise.all(promises)
  .then(() => {
    /* All scripts are loaded, you can execute your functions here */
    single_choice({
      question_code : "Q1", // Question code to configure
      randomize : {         // Settings object for randomize
        answer_groups : [   // List of answer groups to randomize
          [1,2],            // Group 1
          [3,4],            // Group 2
          [5]               // Group 3
        ],
        randomize_groups : true // Randomize groups
      }
    });

    /* All script ends here */
    document.querySelector("body").style.opacity = "1";
  })
  /* If a library could not be imported throw an error */
  .catch((error) => {
    console.error(`Failed to load script: ${error}`);
  });

Last updated

Was this helpful?