I have a form in a modal. I would like to empty the already filled fields and close the modal. How does that work?
Would you like to reset the form and then close the modal in one step?
Or would a button for resetting suffice? I couldnât find one in the Blocs form templates, or perhaps I overlooked it.
But you could use the HTML widget to insert a button
<button type="reset">Reset</button>
It will look different, but you can customize it by using a common class for all buttons.
If you want to do both in one step, it wonât work without JavaScript. Do you use jquery ?
I do now ![]()
I found a solution with AI.
- Step:
Custom interaction âcall javaScriptâ by using this script
"const form = document.getElementById(âform_1â);
const inputs = form.getElementsByTagName(âinputâ);
for (let input of inputs) {
input.value = ââ;
}
const textareas = form.getElementsByTagName(âtextareaâ);
for (let textarea of textareas) {
textarea.value = ââ;
}
const selects = form.getElementsByTagName(âselectâ);
for (let select of selects) {
select.selectedIndex = 0;
}"
2.Step:
toggle modal.
Thatâs it.
Thank you Blocs-Freak
@Bootsie much easier without AI:
document.getElementById("form_1").reset();
It should be added and clarified that HTMLFormElement.reset() simply restores a forms default values, anything that is pre-filled will not be cleared / emptied. The same holds true for button type="reset". Either approach just restores each item to its initial âdefaultâ value. If anything is pre-filled those will need explicitly cleared / emptied, if that is the desire.
If users donât make pre-filled form aspects then its not an issue. ![]()
Thank you Jannis,
the result is the same but your script is much cleaner and easier.
Thatâs exactly what Iâm trying to achieve.
And Jannisâ script does exactly this.
Thank you both.
Then either of the reset approaches mentioned above will work and yield the same result of restoring form controls to their initial values.
HTML : <input type="reset"> - HTML | MDN
Javascript : HTMLFormElement: reset() method - Web APIs | MDN
Rather quiet around the forum lately, so re-read this thread âŚ
Not sure about within Blocs but with vanilla Bootstrap, a button inside a modal:
<button type="reset" data-bs-dismiss="modal" class="btn btn-secondary">Reset & Close</button>
Two key aspects:
Reset: type="reset"
Close: data-bs-dismiss="modal"
No extra JS needed, if thats all you wanât and if it can be done in Blocs.