简体   繁体   中英

How do I create a global variable in Qualtrics Question Text?

How do I create a variable to use in my text using a Qualtrics survey in multiple questions? I am not using any embedded data or making variations. The variable would be for the sole purpose of editing future drafts of my survey more efficiently. For example I want something like:

myvar = 100

Q1. If you have %myvar% and I take away 50 how much is left?

Q2. If you have %myvar% and I give you 70 how much do you get?

...

The more thorough the explanation the better!

This Qualtrics Community answer might help you:

Add a function to the Look & Feel header that sets and returns the variable. Call the function from the JS in your questions.

This support article implies you can add JavaScript to any question that can set global variables, however it isn't recommended:

As with any implementation of JavaScript, we recommend abstaining from global variable creation as it can collide with existing functionality on the page.

Here's the workflow they describe (the below images are from the article):

Click "Add JavaScript" in the settings dropdown on a question, and enter your (global variable assignment) code in one of these functions:

addOnload() – Executed when the page is loaded.

addOnReady() – Executed when the page is fully displayed.

addOnUnload() – Executed when a page is unloaded (when the page is left).


As an aside, here's a good answer from Brian Rasmussen explaining the problems with global variables:

The problem with global variables is that since every function has access to these, it becomes increasingly hard to figure out which functions actually read and write these variables.

To understand how the application works, you pretty much have to take into account every function which modifies the global state. That can be done, but as the application grows it will get harder to the point of being virtually impossible (or at least a complete waste of time).

If you don't rely on global variables, you can pass state around between different functions as needed. That way you stand a much better chance of understanding what each function does, as you don't need to take the global state into account.

You don't need any JavaScript for this. What you want to do is very easy in Qualtrics.

First, at the beginning of your survey flow assign a value to an embedded data field:

myvar = 100

Then pipe the value into your question text like:

Q1. If you have ${e://Field/myvar} and I take away 50 how much is left?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM