Submitting a form with a custom button

When creating an on click function for a button. Are you better off calling it from the onReady function or using the export function option?

I’m creating a button that submits that a form. In the velo documentation the code it has to do in this is

$w(‘#submitButton’).onClick(async () => {
try {
const formValues = await $w(‘#form’).submit();
console.log(‘Form is submitted with the following values’, formValues);
} catch (error) {
console.log(‘Submission failed with an error:’, error);
}
})

I wasn’t sure if this should just go into the on ready function or if it should be coded it like this

export async function submitButton_click(event) {
try {
const formValues = await $w(‘#form’).submit();
console.log(‘Form is submitted with the following values’, formValues);
} catch (error) {
console.log(‘Submission failed with an error:’, error);
}
}

Any help would be greatly appreciated. Thanks.

The first syntax is preferable. The second syntax requires creating an event handler in the Properties and Events panel which is an unnecessary step.

1 Like