SOLVED - Submit button code with events

Hi, I’m creating a user form with text inputs and dropdowns. After hitting submit I want some of them to retain their previous values and some of them cleared and ready for the next input. The ones that retain their previously entered values must change only when the user manually changes them. I tried coding it but it’s overrunning the actual source code of the submit button ie., loading the data into the dataset.
Uploaded a picture to give you a better idea. The ones checked in black should retain the values.
Any help would be much appreciated, thanks.

Hi,
You can try something like this:

  1. Save the values using the “beforeSave” event of the dataset
  2. Re-apply the values in the “afterSave” event

The problem with that is the dataset is resetting the form AFTER you re-apply the values, but as a workaround we can counter that by delaying your code.
Here’s a code example which saves the “age” field and re-applies it. It waits half a second before re-applying, to wait for the dataset to reset the form first:

$w.onReady(function () {
    let lastAge = null;

    $w('#your_dataset_id').onBeforeSave(() => {
        lastAge = $w('#your_dataset_id').getCurrentItem().age;
    });
    $w('#your_dataset_id').onAfterSave(() => {
        setTimeout(() => {
            $w('#your_dataset_id').setFieldValue('age', lastAge);
        }, 500);
    })
});
1 Like

Thanks for responding.
However, isn’t there a way to load the values directly into the user input box instead of defining a variable?
As in,
$w(’ #input1 ‘).value = $w(’ #dataset1 '.)getCurrentItem().buildingHeight;
or something along these lines.
I tried following your example but the form got reset anyway.

@kiransbharadwaj it’s not possible, since after you submit the form, the current item of the dataset resets (which causes the reset of the form) so you have to save it before it’s deleted. If you share your site, maybe someone could take a look to see why it’s not working for you.

@tomer-wix

$w.onReady( function () {
let bheight = null
$w(‘#dataset1’).onBeforeSave(() => {
bheight = $w(‘#dataset1’).getCurrentItem().buildingHeight;
});
$w(‘#dataset1’).onAfterSave(() => {
setTimeout(() => {
$w(‘#dataset1’).setFieldValue(‘buildingHeight’, bheight);
}, 500);
})
});
This is what I did. How do I make sure which field I want to re-apply the value too? As I mentioned in the original post, I have several fields that need to retain the values. Do I assign the input box to the variable in that case?
This is my site : https://vodafoneideaapt.wixsite.com/fieldaudit1

The code looks ok, maybe try to increase the timeout?
Also, you can do it with multiple fields, just do the same thing you do with buildingHeight with the other fields.

1 Like

@tomer-wix
Yes, increasing the timeout did the trick. Works perfectly now.
Thanks a bunch for helping me out :slight_smile: