# How write to a dataset field from a code generated text box?

**URL:** <https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [July 10, 2021, 5:07pm UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701 "2021-07-10T17:07:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericheuser](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@ericheuser](https://forum.wixstudio.com/u/ericheuser)\
**Post date:** [July 10, 2021, 5:07pm UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701/1 "2021-07-10T17:07:40Z")

</div>

I have multiple fields on my page that wright to a dataset with a submit button. I also have code to create a word count in a textbox call count1.  
I would like the number in the wordcount textbox (count1) to be written in the database when I click on the submit button.

I think I would need to change the “onReady” by something else.

 ![](https://us1.discourse-cdn.com/wix/original/3X/8/e/8e71a72a0da1e8487e02e690671a10592661e8bb.png)

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [July 11, 2021, 7:36am UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701/2 "2021-07-11T07:36:35Z")

</div>

If the **total** field in the collection is defined as **Number** , you will need to convert the #count text field to a **Number** in order to save it to the database collection. Do something like this:

```javascript
let wordcount = Number($w('#count').text);
let toInsert = {
   "total" : wordcount
};

```

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [July 11, 2021, 11:52am UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701/3 "2021-07-11T11:52:32Z")

</div>

Also, please observe the [community guidelines](https://www.wix.com/corvid/forum/community-discussion/forum-policies-and-guidelines) and refrain from multiple posts on the same topic or question. Thank you.

---

<div class="post-metadata">

**Author:** ![ericheuser](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@ericheuser](https://forum.wixstudio.com/u/ericheuser)\
**Post date:** [July 11, 2021, 1:14pm UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701/4 "2021-07-11T13:14:33Z")

</div>

Hi Yisrael,  
Thank you for your answer.  
The code is giving me 2 errors:

1- It creates an empty line (See image line 2)  
2- the total field is still empty.

* * *

**import** wixData **from** ‘wix-data’ ;

export function richTextBox1\_keyPress\_1 ( event , $w ) {  
setTimeout (() =\> {  
**let** val = event . target . value ;  
**if** ( val . length === 0 )  
**return** ;  
**let** count = WordCount ( val );  
$w ( “#count1” ). text = “” + count ;  
}, 10 );  
}

**function** WordCount ( str ) {  
**return** str . split ( " " ). length ;  
}

**const** DATABASE = “Scene” // ← put in here your Collection-ID.

$w . onReady (()=\>{ myFunction ();});

**function** myFunction () {

**let** wordcount = Number ( $w ( ‘#count’ ). text );  
**let** toInsert = {  
“total” : wordcount  
};  
wixData . insert ( DATABASE , toInsert )  
. then (( results ) =\> { **let** item = results ;  
console . log ( results ) })  
. **catch** (( err ) =\> { **let** errorMsg = err ; });  
}

* * *

 ![](https://us1.discourse-cdn.com/wix/original/3X/4/4/4440db19a0b52ee6303b2e2c35242257ed053877.png)

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [July 11, 2021, 1:58pm UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701/5 "2021-07-11T13:58:49Z")

</div>

You should realize that in order to create a line in the database collection, it’s not enough to just include the total field - you will need to include all of the fields that you want to insert in the **toInsert** object.

Perhaps $w ( ’ [#count](https://www.wix.com/velo/forum/search/~num~count) ’ ) has no value which is why that field is empty as well? Try a console.log() statement to see what the value is.

If you are still having problems, please post the URL and explain where and how to see the problem.

---

<div class="post-metadata">

**Author:** ![ericheuser](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@ericheuser](https://forum.wixstudio.com/u/ericheuser)\
**Post date:** [July 11, 2021, 2:34pm UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701/6 "2021-07-11T14:34:09Z")

</div>

I don’t understand why I need this part of the code that isn’t linked to the submit button. It sends the info when I load the page and no data has been entered yet… Why doesn’t the submit button simply send the data from this textbox like all the other textboxes on the page. It’s like since the wordcount was entered automatically from a script and not entered from a keyboard it doesn’t see it…

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [July 11, 2021, 2:56pm UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701/7 "2021-07-11T14:56:16Z")

</div>

Aha - so you have a dataset. I didn’t understand what you were trying to do since you were trying to insert **wordcount** using **wix-data.insert()**.

In order to save **wordcount** to the **total** field in the collection via the **Submit** button, you need to set the dataset field value using [setFieldValue()](https://www.wix.com/velo/reference/wix-dataset/dataset/setfieldvalue) . You can do something like this:

```javascript
$w("#dataset1").setFieldValue("total", Number($w('#count').text));

```

Then when the Submit button is clicked, all of your fields, including total, will be saved to the database collection.

Note: It is recommended not to mix wix-data code together with wix-dataset code.

---

<div class="post-metadata">

**Author:** ![ericheuser](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@ericheuser](https://forum.wixstudio.com/u/ericheuser)\
**Post date:** [July 11, 2021, 3:54pm UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701/8 "2021-07-11T15:54:54Z")

</div>

Thanks Yisrael, You’re a genius! It worked! I had been struggling with this for a while now.

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [July 11, 2021, 4:00pm UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701/9 "2021-07-11T16:00:40Z")

</div>

@ericheuser If I was a genius, I would’ve figured it out earlier. 🍻

---

<div class="post-metadata">

**Author:** ![ericheuser](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@ericheuser](https://forum.wixstudio.com/u/ericheuser)\
**Post date:** [July 11, 2021, 4:04pm UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701/10 "2021-07-11T16:04:58Z")

</div>

Where would be the best place to learn this coding… It looks like JavaScript but not exactly…

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [July 11, 2021, 4:21pm UTC](https://forum.wixstudio.com/t/how-write-to-a-dataset-field-from-a-code-generated-text-box/37701/11 "2021-07-11T16:21:45Z")

</div>

See [Coding with Velo](https://support.wix.com/en/velo-by-wix/coding-with-velo) for a quick introduction to Velo Javascript.

To learn about programming with Velo, read the following articles that will help you start working with Velo:

- [About Velo by Wix](https://support.wix.com/en/article/about-corvid-by-wix) - what Velo is and what features it has.

- [Getting Started with Velo by Wix](https://support.wix.com/en/article/getting-started-with-corvid-by-wix) - step-by-step tutorial on how to start using Velo.

- [Onboarding to Velo by Wix](https://support.wix.com/en/article/onboarding-to-corvid-by-wix) - introduction to Velo with short tutorials.
