| Reference | Downloads | Github

Cannot read property '0' of undefined


URL of experiment:


Experiment created using builder on Windows, version 3.0.4.

When running my experiment I get the following error:


Looking online I suspect the error could be related to a code component in my builder which allows me to register and display participant input, live. The Python code is the following (from:

#Begin experiment
inputText = ""

#Begin routine
shift_flag = False
txtinp.alignHoriz ='center'

#Each frame
n= len(theseKeys)
i = 0
while i < n:

    if theseKeys[i] == 'return' and len(inputText) >1:
        # pressing RETURN means time to stop
        continueRoutine = False

    elif theseKeys[i] == 'backspace':
        inputText = inputText[:-1]  # lose the final character
        i = i + 1

    elif theseKeys[i] == 'space':
        inputText += ' '
        i = i + 1

    elif theseKeys[i] in ['lshift', 'rshift']:
        shift_flag = True
        i = i + 1

        if len(theseKeys[i]) == 1:
            # we only have 1 char so should be a normal key, 
            # otherwise it might be 'ctrl' or similar so ignore it
            if shift_flag:
                inputText += chr( ord(theseKeys[i]) - ord(' '))
                shift_flag = False
                inputText += theseKeys[i]

        i = i + 1

#End routine
# let's store the final text string into the results file...
thisExp.addData('inputText', inputText)

The custom code is part of this full structure, where “textinp” displays “$inputText” from the code above.

My js-“translation” is a simple code:

#Begin experiment
function inputText() {
  var x = document.createElement("INPUT");
  x.setAttribute("type", "text");
  x.setAttribute("value", "");

So I suspect the problem is my js-code. However, as far as I know it could be something else entirely. Any help would be greatly appreciated


Hi @Nessimon, I think one problem you will have is using a numpy method to select your rows. Numpy is a Python library, so you will have to find an alternative method for choosing your rows for the JavaScript experiment.


Thanks a lot! That worked, now I can get to the first part of my experiment:

But I believe the above is still a problem, I now get the error message:

Thanks again.


Great. Looks like your function inputText is not available globally, so when you use inputText, it appears as not defined. To get PsychoPy to do this for you, just create an inputText variable before you create the function e.g., inputText = '';. That way, inputText should be defined globally outside of the experimentInit function.


Thank you! Now the error message has disappeared, but it still does not register my keyboard input and display it, like my python code does. Any suggestions?



I think you need to work on your JS code and make use of the PsychoJS text objects, as you would with Python, rather than attempting to manipulate text using HTML commands.


Here is an example of how you would get typed text to appear on screen online. Assuming you have a text component called “text”, and a code component - in “Every Frame”:

let theseKeys = psychoJS.eventManager.getKeys();
if (theseKeys.length > 0) {  // at least one key was pressed
  textAdd = theseKeys[theseKeys.length-1]; 

// check for quit:
if (theseKeys.indexOf('escape') > -1) {
    psychoJS.experiment.experimentEnded = true;
if (textAdd) {
    text.setText(text.text + textAdd);  
    textAdd = undefined;