Setting variable names dynamically using JS
Hey Guys,
I'm right now setting up an experiment where I need to dynamically define text stimuli presented in a sketchpad. The stimuli all come from three different lists and are placed on the sketchpad dependent on their list location. To prepare the stimuli for the visual presentation on the sketchpad I need to dynamically create variables - where the variable name is depending on the location list and the specific place inside the list. Yet I managed to do so in python using a var.set() command. Now as I try to set the experiment up online I'm having some trouble translating my python solution to JS.
Here is a code snipped of the python solution, followed by my current JS approach:
As you can see what I yet have tried is to define the name variable as a list and then try to use it's content (content) as a variable name. Though this does not work - probably because all it does is to overwrite the content of the name (variable rank_var_name[0]).
Do you have any suggestion how I can set the variable names to the content of my name variable (rank_var_name) in javascript?
Thanks in advance,
Leo
Comments
Hi @lt_kn_char ,
In OpenSesame 4.0, which I recommend for new experiments, you don't need the
varorvarsobject anymore, because experimental variables are simply available as global variables ininline_scriptandinline_javascriptitems. See:The JavaScript way to set variables that themselves have a variable name is using the
windowobject, which is a container that holds all global variables.This means that the equivalent of:
Would be:
Hope this helps!
— Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
This works perfect, thank you!