# Copy the value from #input1 to #input2 and write it to the database

**URL:** <https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907>\
**Category:** Ask the community\
**Tags:** question, wix-editor, cms, code\
**Created:** [July 16, 2024, 8:39am UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907 "2024-07-16T08:39:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Loki2k3](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/loki2k3/32/92209_2.png) [@Loki2k3](https://forum.wixstudio.com/u/Loki2k3)\
**Post date:** [July 16, 2024, 8:39am UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/1 "2024-07-16T08:39:08Z")

</div>

Hello,

I’m experiencing issues with CMS databases and Velo code programming.

The value from the left input field is being captured, but it isn’t writing to the database.

In the first part of the video, I input a value on the left, which is supposed to be transferred to the right field. It does so successfully. However, it does not write this value to the database.

In the second part, when I only input a value into the right field, it writes to the database.

I have tried many different approaches, but it simply does not write the value it retrieves from another field into the database. For some reason, it just doesn’t work.

Below is my code:

```javascript
$w.onReady(function () {
    const handleInputChange = () => {
        const input1Value = $w('#input1').value.trim();
        console.log("Input1 value:", input1Value);

        $w('#input2').value = input1Value;

        console.log("Input2 value after update:", $w('#input2').value);
    };

        $w('#input1').onChange(handleInputChange);    
});

```

Is the mistake on my end, or is this a WIX error?

---

<div class="post-metadata">

**Author:** ![Pratham](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/pratham/32/119586_2.png) [@Pratham](https://forum.wixstudio.com/u/Pratham)\
**Post date:** [July 16, 2024, 10:12am UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/2 "2024-07-16T10:12:57Z")

</div>

Everything’s perfect, just add

`$w('#input2').focus();`

after

`$w('#input2').value = input1Value;`

> [@Unable to Retrieve Input Value](https://forum.wixstudio.com/t/unable-to-retrieve-input-value/52448/2):
>
> No actually its not a dynamic page issue, it is apparently a bug with all input fields. So unless the user takes their cursor over the input box and clicks on it, the input does not consider it as the value you’ve set by code. I was facing the same issue but I managed to solve it using a simple workaround. Try calling the $w('#input').focus(); after assigning a value to the input or before reading the value of that input. This should solve the issue.

---

<div class="post-metadata">

**Author:** ![Loki2k3](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/loki2k3/32/92209_2.png) [@Loki2k3](https://forum.wixstudio.com/u/Loki2k3)\
**Post date:** [July 16, 2024, 11:16am UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/3 "2024-07-16T11:16:27Z")

</div>

Thank you for your response, Pratham. I tried both variants, before and after the value was adopted. The database field remains empty.

**TEST 1:**

```javascript
$w.onReady(function () {
    const handleInputChange = () => {
        const input1Value = $w('#input1').value.trim();
        console.log("Input1 value:", input1Value);
                $w('#input2').value = input1Value;
        $w('#input2').focus();

        console.log("Input2 value after update:", $w('#input2').value);
    };

    $w('#input1').onChange(handleInputChange);
});

```

**TEST 2:**

```javascript
$w.onReady(function () {
    const handleInputChange = () => {
        const input1Value = $w('#input1').value.trim();
        console.log("Input1 value:", input1Value);
                $w('#input2').focus();
                $w('#input2').value = input1Value;
        

        console.log("Input2 value after update:", $w('#input2').value);
    };

    $w('#input1').onChange(handleInputChange);
});

```

Look this Video… [https://youtu.be/5jLeI8ZCDGk](https://youtu.be/5jLeI8ZCDGk)

Only if I manually add a space or a letter again, the value is accepted.  
Simply clicking on the input field does not bring any improvement for me.

---

<div class="post-metadata">

**Author:** ![Loki2k3](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/loki2k3/32/92209_2.png) [@Loki2k3](https://forum.wixstudio.com/u/Loki2k3)\
**Post date:** [July 16, 2024, 11:28am UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/4 "2024-07-16T11:28:23Z")

</div>

One more thing.

When working with a form and input fields, it works without any problems. Strange…

But that doesn’t help me in this case right now ☹

---

<div class="post-metadata">

**Author:** ![Pratham](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/pratham/32/119586_2.png) [@Pratham](https://forum.wixstudio.com/u/Pratham)\
**Post date:** [July 16, 2024, 3:17pm UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/5 "2024-07-16T15:17:06Z")

</div>

> [@Loki2k3](#):
>
> Only if I manually add a space or a letter again, the value is accepted.

Yes, that is because you added `$w('#input2').focus();` **before** `$w('#input2').value = input1Value;` and not **after** , like I had told you to 🙈

Don’t worry I’ve included the whole code below so you can just copy paste it.

Also one more thing that I’d recommend is to use **onInput instead of onChange**. This is just to be sure in case the user directly clicks on the submit button while their cursor is still in the input box, there is a chance that the onChange event may not be fired at all. So here’s the updated code, this should work perfectly:

```javascript
$w.onReady(function () {

    const handleInputChange = () => {
        const input1Value = $w('#input1').value.trim();
        $w('#input2').value = input1Value;
        $w('#input2').focus();
    };

    $w('#input1').onInput(handleInputChange);
});

```

---

<div class="post-metadata">

**Author:** ![Loki2k3](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/loki2k3/32/92209_2.png) [@Loki2k3](https://forum.wixstudio.com/u/Loki2k3)\
**Post date:** [July 16, 2024, 5:57pm UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/6 "2024-07-16T17:57:02Z")

</div>

Pratham, thank you once again for taking the time and sharing your knowledge.

I’ve copied the code exactly as you provided. When using `onInput`, it jumps directly to the second cell even though I’m not done typing.

Changing it to `onChange` prevents this, but then it doesn’t write anything to the database again.

I’m using the Wix Editor. Are you using the same or are you working with Wix Studio? Maybe that’s causing the issue? It’s really frustrating because the code should work, but it seems like there might be something off with the input field on Wix’s end, I believe…

Video= [https://youtu.be/AtencsvY98s](https://youtu.be/AtencsvY98s)

---

<div class="post-metadata">

**Author:** ![Pratham](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/pratham/32/119586_2.png) [@Pratham](https://forum.wixstudio.com/u/Pratham)\
**Post date:** [July 16, 2024, 7:20pm UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/7 "2024-07-16T19:20:01Z")

</div>

Hi again,

And thanks to you too for creating the screen recordings and uploading it to YouTube describing the issue. I don’t know why your posts got flagged though, since the Wix Forum itself does not allow sharing videos files.

Coming back to the issue -

> [@Loki2k3](#):
>
> When using `onInput`, it jumps directly to the second cell even though I’m not done typing.

That’s something I totally overlooked, apologies for that.

> [@Loki2k3](#):
>
> I’m using the Wix Editor. Are you using the same or are you working with Wix Studio? Maybe that’s causing the issue? It’s really frustrating because the code should work, but it seems like there might be something off with the input field on Wix’s end, I believe…

I wasn’t trying it out on my end yet, since this seemed to be a very straightforward bit of code, but because it did not work, I created a test page on my end to replicate the issue and guess what - I’m facing the exact same problem. I tried many different approaches using onBlur, onChange, onInput… but nothing seems to work. I am getting a blank entry just like you. So this is definitely something wrong at Wix’s end.

But the good news is I’ve come up with an alternate way to achieve this, and that is to intercept the dataset directly, bypassing the input field altogether. For this you will have to **disconnect** the second input field as we will not be using this to submit data, but just as a visual feedback. The code is fairly simple and will directly assign the value to the particular field as you type it in _input1_. This works like a charm.

```javascript
$w.onReady(function () {
    $w('#input1').onInput(() => {
        $w("#dataset1").setFieldValue("title", $w('#input1').value.trim());
        $w('#input2').value = $w('#input1').value.trim();
    });
});

```

Here’s the page that I created in the classic editor, that uses the above code:

> **[Multi-input fields](https://preztech.editorx.io/lightning/ur/39dab941-0955-432c-8c84-70045157acee)**
>
> This is a test website created by PREZTECH to showcase how to intercept the dataset directly.

Couple of things to note:

- Make sure the second input field is no longer connected to the dataset.
- Make sure your Dataset ID is correct, especially when you have multiple datasets on the page.
- **“title”** is the Field ID of the particular column in the CMS. You will see it if you have Dev Mode turned on. In case you want to link this to another column, simply replace _title_ with the ID of the respective field as shown below:

 ![Screenshot 2024-07-17 at 12.49.21 AM](https://us1.discourse-cdn.com/wix/original/3X/a/2/a2ff02190975af94d6771b36b9ff727cb5e45ddd.png)

---

<div class="post-metadata">

**Author:** ![Loki2k3](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/loki2k3/32/92209_2.png) [@Loki2k3](https://forum.wixstudio.com/u/Loki2k3)\
**Post date:** [July 16, 2024, 7:51pm UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/8 "2024-07-16T19:51:50Z")

</div>

Yes, I also don’t know why the posts were marked, using videos can visualize issues much better.

That’s fantastic, Pratham! It works.

I have also written to WIX about this. Maybe they will fix it in the future. But your code bypasses this error flawlessly, and it also means I need significantly fewer input fields for my project.

**!!! Thank you, Pratham !!!**

---

<div class="post-metadata">

**Author:** ![Pratham](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/pratham/32/119586_2.png) [@Pratham](https://forum.wixstudio.com/u/Pratham)\
**Post date:** [July 16, 2024, 7:56pm UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/9 "2024-07-16T19:56:36Z")

</div>

Happy to help! Cheers 😊

---

<div class="post-metadata">

**Author:** ![Loki2k3](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/loki2k3/32/92209_2.png) [@Loki2k3](https://forum.wixstudio.com/u/Loki2k3)\
**Post date:** [July 17, 2024, 3:22pm UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/10 "2024-07-17T15:22:51Z")

</div>

Hi Pratham, I reported the bug to Wix. That is the answer. Can you explain to me what they mean by that?

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

---

<div class="post-metadata">

**Author:** ![Pratham](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/pratham/32/119586_2.png) [@Pratham](https://forum.wixstudio.com/u/Pratham)\
**Post date:** [July 18, 2024, 5:07am UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/11 "2024-07-18T05:07:03Z")

</div>

It’s basically telling us to do what we just did. 🙂

So this proves that input fields connected to a dataset cannot have their value edited by just changing the value of the input fields. But when you’re using **wix-data** instead of a dataset, that works absolutely well.

Nevertheless, I’ll keep this in mind. Thanks for sharing this Michael! (:

---

<div class="post-metadata">

**Author:** ![Loki2k3](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/loki2k3/32/92209_2.png) [@Loki2k3](https://forum.wixstudio.com/u/Loki2k3)\
**Post date:** [July 18, 2024, 6:16am UTC](https://forum.wixstudio.com/t/copy-the-value-from-input1-to-input2-and-write-it-to-the-database/60907/12 "2024-07-18T06:16:44Z")

</div>

Sure, Pratham.

I tested something else. If you integrate the fields into a form and then take the value using “handleInputChange” and submit the form at the end, it transfers the values to the CMS. Outside of the form, it only works using your method.

Best regards, Michael
