# Textbox Output Merging words

**URL:** <https://discourse.psychopy.org/t/textbox-output-merging-words/43886>\
**Category:** Online experiments\
**Created:** [February 13, 2025, 12:46pm UTC](https://discourse.psychopy.org/t/textbox-output-merging-words/43886 "2025-02-13T12:46:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![allenjj2](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@allenjj2](https://discourse.psychopy.org/u/allenjj2)\
**Post date:** [February 13, 2025, 12:46pm UTC](https://discourse.psychopy.org/t/textbox-output-merging-words/43886/1 "2025-02-13T12:46:13Z")

</div>

Description of the problem:

Hi,

I’m coding a free-recall memory experiment where participants type words into a TextBox component.

**What I’m hoping to achieve**

- Participants should be able to enter multiple words, either by pressing space or pressing enter to move to a new line.
- In the csv output file, words should be stored separately (e.g. “dog cat bird” or [“dog”, “cat”, “bird”)

**Problem**  
Currently, if participants press ‘enter’ between words instead of space, the output is saved as one continuous string without spaces (e.g. “dogcatbird” instead of “dog cat bird”).

I’m not that comfortable or familiar with Javascript. However, I did try using the following piece of code to split by spaces and newline characters;

user\_answer = textboxRetrieval.text.split(/\s+/);

Unfortunately, this did not work.

**Question**  
Is there a way to handle both spaces and enter presses, or is this just something that I will need to tackle when it comes to cleaning the data?

---

<div class="post-metadata">

**Author:** ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)\
**Post date:** [February 13, 2025, 2:06pm UTC](https://discourse.psychopy.org/t/textbox-output-merging-words/43886/2 "2025-02-13T14:06:56Z")

</div>

I’m not 100% sure how the text box actually records “enter” presses, but if they are standard newline symbols, you could try this:

```auto
user_answer = textboxRetrieval.text.split(/[\s\n]+/);

```

Square brackets mean “one of these”, so it should split on either a space or a line-break.

---

<div class="post-metadata">

**Author:** ![allenjj2](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@allenjj2](https://discourse.psychopy.org/u/allenjj2)\
**Post date:** [February 14, 2025, 9:53am UTC](https://discourse.psychopy.org/t/textbox-output-merging-words/43886/3 "2025-02-14T09:53:18Z")

</div>

Thank you for the suggestion!

Unfortunately, it doesn’t appear to fix the issue.

I used console.log(JSON.stringify(user\_answer)) to see whether the textbox is recording “enter” presses as newlines, and it does. Therefore, I’m not completely sure why splitting does not work?

---

<div class="post-metadata">

**Author:** ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)\
**Post date:** [February 14, 2025, 10:07am UTC](https://discourse.psychopy.org/t/textbox-output-merging-words/43886/4 "2025-02-14T10:07:47Z")

</div>

Interesting. How does it look if you do `console.log(textboxRetreival.text)` instead? The same?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [February 14, 2025, 10:45am UTC](https://discourse.psychopy.org/t/textbox-output-merging-words/43886/5 "2025-02-14T10:45:40Z")

</div>

I have an idea to solve this but I haven’t had a chance to test it yet.

Add a keyboard component that monitors for return and Each Frame code which adds a space at or near the end of textbox.text when a new return is pressed.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [February 14, 2025, 10:02pm UTC](https://discourse.psychopy.org/t/textbox-output-merging-words/43886/6 "2025-02-14T22:02:48Z")

</div>

This turned out to be significantly harder to solve than I was expecting, but my final solution is more elegant than the one I had to abandon.

What I discovered is that textbox.text is not the same during the routine as when it ends. That’s the point at which the newline characters get stripped out. My first attempt was fine for every word apart from the last one, where it lost most or all of the letters.

My tip for today is how to define a split function so you can use an Auto code component in the trial routine, and how to call the split function as the routine is ending, before the newline characters have been stripped out.

> [@Wakefield's Daily Tips](https://discourse.psychopy.org/t/wakefields-daily-tips/43371/46):
>
> Splitting textbox.text online If you want to split the text from an editable textbox into words (for example in a free recall task) online, there there are two issues that need to be overcome. The Python function .split() which splits on any whitespace is auto translated into .split(" ") which only splits on spaces. The text in the data file has no new line breaks at all. It appears the the newline characters in an editable textbox are stripped from the text when the routine ends. To so…

---

<div class="post-metadata">

**Author:** ![allenjj2](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@allenjj2](https://discourse.psychopy.org/u/allenjj2)\
**Post date:** [February 17, 2025, 9:37am UTC](https://discourse.psychopy.org/t/textbox-output-merging-words/43886/7 "2025-02-17T09:37:17Z")

</div>

Thank you for taking the time to look into this. This solution worked perfectly!
