Back to Help
Question Types

Custom Code in Survey Questions

Use custom HTML, JavaScript variables, and Python validation only when standard settings are not enough.

When To Use Custom Code

Most research surveys should use built-in question settings. Custom code is for advanced instruments that need presentation, validation, or variables beyond standard configuration. Use it sparingly and test thoroughly before publishing.

Before adding code, check whether the same result can be handled with validation rules, Flow, condition assignment, or a standard question type. Custom code should have a clear owner and a clear reason.

Open The Right Place

  1. Step 1: Open the survey editor. From Surveys, open the draft that needs advanced behavior.

    Step 1: Open the survey editor

  2. Step 2: Start in Block Builder. Add or select the question where the custom HTML, JavaScript, or validation belongs.

    Step 2: Start in Block Builder

  3. Step 3: Use Flow only for routing logic. Open Flow when a custom variable should assign respondents to a condition arm or branch.

    Step 3: Use Flow only for routing logic

  4. Step 4: Verify in Preview and Export. Use Preview to test behavior, then submit a test response and review the exported values.

    Step 4: Verify in Preview and Export

Available Advanced Uses

  • Custom HTML: add specialized participant-facing markup when a normal prompt is not enough.
  • Custom JavaScript: declare variables that can support advanced survey behavior or exports.
  • Python validation: validate Text Response responses with custom server-side logic.
  • Python assignment rules: support advanced by-variable matching for condition arms.

Options: Text Response Validation

How Question JavaScript Reads And Creates Variables

Open a question in Block Builder, expand Custom code, then expand JavaScript. In the default sandbox, each script can read two objects: answers and vars.

  • answers contains the answers currently available in the survey, keyed by question ID. Read one with answers["question-id"]. The value depends on the question type, and it may be undefined if the respondent has not answered that question yet. The key is the internal question ID, not its prompt or analysis variable name; you can find IDs in the Domandata JSON export.
  • vars contains resolved Flow variables and values created by question JavaScript. Read a named value with vars.variable_name. Flow variables are the convenient way to refer to an answer by a researcher-chosen name rather than a question ID.

To create a value for later questions, declare it with let, const, or var. Domandata detects the declared name and adds it to Flow as a read-only question JavaScript variable. It can then be used by later scripts, branching, piping, and exports.

let eligible = answers["SCREENING_QUESTION_ID"] === "yes";

Replace SCREENING_QUESTION_ID with the source question's ID, and replace yes with the answer value that question stores. If you already have a Flow variable named screening_result, you can read it like this:

let eligible = vars.screening_result === "yes";

A declared JavaScript value named eligible can be piped later as {{var:eligible}}. If a value is only needed for display or routing, consider creating it with the built-in Flow tools instead; those are easier for collaborators to inspect.

Sandbox And Page Access Modes

By default, Custom JavaScript runs in an isolated sandbox. It can read and update its answer and variable data, but it cannot inspect or change the survey page or make network requests. Scripts have a 250 millisecond execution budget; if a script times out or errors, its updates for that run are not applied.

The optional Custom JavaScript with page access setting is in the survey's General options. It requires a dedicated respondent origin; it will not run on the main Domandata app origin. When enabled, question scripts run against the real respondent page, so they can read answers, manipulate the page, and make network requests. Only turn it on for code you wrote or fully trust. This mode uses a different API: the script receives a question object instead of the sandbox's answers and vars arguments.

  • question.answers is a snapshot of the current answers.
  • question.getAnswer() reads this question's current answer; question.getAnswer("question-id") reads another question's answer.
  • question.setVariable("eligible", value) writes a Flow variable for piping and branching.
  • question.container is this question's DOM element. Use it to keep page changes scoped to the question.

Custom HTML by itself is display-only. Domandata sanitizes it: only a limited set of markup is kept, and classes, IDs, data attributes, inline styles, event handlers, scripts, and unsupported tags such as button are removed. There is no CSS editor for this field.

With page access enabled, JavaScript can make the rendered question interactive. Use question.onReady to attach click or change listeners when the question appears, and update the content or styles inside question.container in those handlers. The respondent page permits inline styles, so a script can set an element's style properties or create a styled element itself. The script runs when the page mounts; it does not rerun automatically on each answer change, so use an event listener for updates that should happen immediately.

This can make a small custom interaction, such as selectable links styled as choices. Save the selected value with question.setVariable. That creates a Flow variable; it does not automatically create a native Multiple Choice answer with the built-in requiredness, validation, recodes, and answer-option reporting. Use a standard Multiple Choice question when you need those features.

<a href="#choice-A">Option A</a><a href="#choice-B">Option B</a>
const priorAnswer = question.getAnswer("SCREENING_QUESTION_ID");question.setVariable("eligible", priorAnswer === "yes");

The question JavaScript can save the selected option like this:

question.onReady(() => {question.container?.querySelectorAll('a[href^="#choice-"]').forEach((link) => {link.addEventListener("click", (event) => {event.preventDefault();const choice = link.getAttribute("href")?.slice("#choice-".length);question.setVariable("module_choice", choice);});});});

The editor's Preview runs the isolated sandbox. Use Runner View to check page-access behavior, then submit a test response and inspect the export before publishing.

Best Practices

  • Start with built-in settings: use custom code only when standard question settings do not meet the protocol.
  • Keep the scope small: make the code short, documented, and easy for collaborators to review.
  • Keep JavaScript fast: each run of a question's JavaScript has a quarter of a second to finish, not counting the moment the isolated sandbox takes to start. A script that runs longer is stopped and its variables are not updated for that run.
  • Preview dependent paths: test every path that depends on custom variables, validation, or assignment rules.
  • Inspect exports: submit test responses and confirm custom values appear in the fields your analysis expects.
  • Protect secrets: do not store API keys, tokens, or private credentials in participant-facing survey configuration.

Preview: Respondent Experience

Research Design Notes

Custom code can make a survey harder for collaborators to audit. Document why it exists, what variables it creates, how it fails, and how it affects respondent flow or data exports.

  • Failure path: decide what respondents see if validation or assignment code rejects an answer.
  • Versioning: record code changes before pushing them to a published survey.
  • Review: ask a collaborator to read any code that affects eligibility, incentives, assignment, or primary outcomes.
  • Export audit: keep a test response that proves the custom value appears where analysis expects it.

Preview: Respondent Experience

Related Help