"If" does not update my collection

I have a form. If a patient has not entered any allergies into the $w( ‘#allergies’ ).value field on clicking the Submit button, I want the $w( ‘#allergies’ ).value in the collection to update to “Sin alergias conocidas”

export function submit_click(event) {
if ($w( ‘#allergies’ ).value === “” ) {
$w( ‘#allergies’ ).value = “Sin alergias conocidas” ;
}
}

Any help would be greatly appreciated.

I don’t see your code for saving to collection.

Does the submit button not save it for me?

@digitalzemb it probably saved before executing the onClick function.
You should disconnect this button from the dataset and submit using code.

Alternatively, you can move the code to a beforeInsert hook in the backend data.js file.

@jonatandor35 Thanx… Still learning about beforeInsert. I have many fields that need to be added to the collection as the record is saved. But when I use the “If” statement to update the one particular field… it doesn’t get saved with the rest of the data.

@digitalzemb as I said you can save the field that way.
you should read:
https://www.wix.com/corvid/reference/wix-dataset/dataset/setfieldvalues

https://www.wix.com/corvid/reference/wix-dataset/dataset/save

@jonatandor35 You are a star. Thanx. I have so many questions but will post them one by one.

You’re welcome :slight_smile:

@jonatandor35 Got it working.
I setFieldValues to update that one field $w( ‘#allergies’ ).value called “allergies” with “Sin alergias conocidas”
ie. “allergies” : “Sin alergias conocidas”

My code:

$w.onReady( function () {
const allergy = $w( ‘#allergies’ ).value;
if (allergy === “” ){
$w( “#ptDataset” ).onReady( () => {
$w( “#ptDataset” ).setFieldValues( {
“allergies” : “Sin alergias conocidas”
} );
} );
}
});