Svelte form data is lost when components are hidden

I am using Svelte and Flowbite components (basically a tailwind-css wrapper) to create a form. I put some of my form inputs into an accordion element which can be toggled to display or not. Now I notice when I fill in the form details, then hide the component, all input data is lost because all elements are actually removed from the DOM. This is catastrophic for me, is there some way to tell svelte not to do this?

>Solution :

You can’t if you not the author and the components do not offer properties to change the behavior. They likely use {#if} internally which add/removes the contents rather than using classes.

As a consumer, you can use bind:property (e.g. on value, checked, etc.) on the inputs of the form to locally store the values, that way, when the form is recreated it will be populated with said values again.

Leave a Reply