.join doesn't work in pavlovia

I am trying to get my experiment online. Unfortunately, I am very unfamiliar with JS and currently running into some issue regarding the improper translation from Python to JS.

URL of experiment: Katharina Weiss / SC · GitLab

Description of the problem:
I have an empty textbox component (textbox_E1) which should receive keyboard input from the participant. Via the code component I only want to allow numbers.

Python Code:

keys = k.getKeys(keyList = [0,1,2,3,4,5,6,7,8,9, 'space']); 

for ch in textbox_E1.text:
   textbox_E1.text = "".join([ch for ch in textbox_E1.text if ch in ["0","1","2","3","4","5","6","7","8","9"]]);
   textbox_E1.text = textbox_E1.text.replace(' ', '')

JS Code:

keys = k.getKeys({"keyList": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "space"]});
for (var ch, _pj_c = 0, _pj_a = textbox_E1.text, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
    ch = _pj_a[_pj_c];
    textbox_E1.text = (function () {
    var _pj_d = [], _pj_e = textbox_E1.text;
    for (var _pj_f = 0, _pj_g = _pj_e.length; (_pj_f < _pj_g); _pj_f += 1) {
        var ch = _pj_e[_pj_f];
        if (_pj.in_es6(ch, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"])) {
    return _pj_d;
    textbox_E1.text = textbox_E1.text.replace(" ", "");

I am getting the following error: TypeError: _pj_d.push(…).join is not a function

I’ve read the crib sheet which pointed me to use array.join(“delimeter”); in the JS code. Have I not implemented this correctly?

I am happy to receive any input! :slight_smile:

Thanks in advance!



Try to separate (in python) the list comprehension from the join function i.e. write these into two different lines. Then whatever is the js translation modify the join function in js as per crib sheet


@Yiannis thanks for your suggestion!

I’ve rewritten the python code to the following (and adjusted the js as per crib sheet):

for i in textbox_E1:
    if i in ["0","1","2","3","4","5","6","7","8","9"]:
        textbox_E1 = "".join([i for i in textbox_E1])
        textbox_E1 = textbox_E1.replace(' ', '')

Is this what you meant?

unfortunately, I am getting the same error ( TypeError: _pj_d.push(…).join is not a function) :frowning:

Hi @Kathi I have just re-read your original post and I think I understand what you are trying to achieve.

If my understanding is correct then what you need is:

  • a textbox component (set the text field to every repeat if in a loop and leave it empty)
  • a keyboard component (in the data tab set to store all keys if Ps are required to type more than one character or to last/first key if they are required to type a single character - and set as allowed keys 0-9)
  • a code component that will contain the below in the every frame tab (change accordingly if your components are named differently)
# python bit
if key_resp.keys:
    textbox.text = "".join(key_resp.keys)
    textbox.text = ''
# js bit
if (key_resp.keys) {
    textbox.text = key_resp.keys.join("");
} else {
    textbox.text = '';

I also attach a demo

I hope this helps

demo_txt.psyexp (10.4 KB)

1 Like

Hi @Yiannis, thanks again!

this works to some extent. Unfortunately it’s not possible now to delete the characters in case the participant would like to change their answer. Do you know how to include this?

I’ve tried to include “backspace” as allowed key for the keyboard component:

plus some code like this:

if len(textbox.text) > 0 and key_resp.keys == 'backspace':    
    textbox.text = ""
elif key_resp.keys:    
    textbox.text = "".join(key_resp.keys)

However, now when I hit backspace, it just prints “backspace” next to the numbers I just put in.

Hi @Kathi ,

Yes this is correct. I have altered it so that it only accepts numbers and space (attached). The keyboard component now only serves to end the trial by pressing enter (return)

The new code component has an allowedList list in the begin routine tab.
In the every frame tab the code snippet checks if any pressed key is part of the allowedList. If not, it removes it.

I am afraid no time to test it online

demo_txt.psyexp (11.9 KB)


@Yiannis this works great! thanks for your help :pray:

1 Like