Reset modal form
August 29, 2025 Β· AI Conversations
<script>
document.addEventListener("DOMContentLoaded", function () {
const steps = document.querySelectorAll(".step");
let current = 0;
function showStep(index) {
steps.forEach((s, i) => s.classList.toggle("active", i === index));
}
// attach click to all .nextandsubmit buttons
document.querySelectorAll(".nextstep").forEach(btn => {
btn.addEventListener("click", function () {
if (current < steps.length - 1) {
current++;
showStep(current);
} else {
// last step β submit form
document.getElementById("msForm").submit();
}
});
});
// show only first step on load
showStep(current);
});
</script>
also add prev step in next step
Hereβs your updated script with both **Next** and **Prev** support:
<script>
document.addEventListener("DOMContentLoaded", function () {
const steps = document.querySelectorAll(".step");
let current = 0;
function showStep(index) {
steps.forEach((s, i) => s.classList.toggle("active", i === index));
}
// NEXT buttons
document.querySelectorAll(".nextstep").forEach(btn => {
btn.addEventListener("click", function () {
if (current < steps.length - 1) {
current++;
showStep(current);
} else {
// last step β submit form
document.getElementById("msForm").submit();
}
});
});
// PREV buttons
document.querySelectorAll(".prevstep").forEach(btn => {
btn.addEventListener("click", function () {
if (current > 0) {
current--;
showStep(current);
}
});
});
// show only first step on load
showStep(current);
});
</script>
### How to use in HTML:
<div class="step active">Step 1
<button type="button" class="nextstep">Next</button>
</div>
<div class="step">Step 2
<button type="button" class="prevstep">Prev</button>
<button type="button" class="nextstep">Next</button>
</div>
<div class="step">Step 3
<button type="button" class="prevstep">Prev</button>
<button type="button" class="nextstep">Submit</button>
</div>
π Each step can have a **.prevstep** (except the first) and a **.nextstep** (except the last if you only want submit).
Do you also want me to make the **Prev button hidden on the first step** and **Next button hidden on the last step** automatically?
Hereβs the script to do that:
<script>
document.addEventListener("DOMContentLoaded", function () {
const modal = document.getElementById("addWorkerModal");
const form = document.querySelector("#addWorkerModal form");
// When modal is opened β reset form
modal.addEventListener("show.bs.modal", function () {
if (form) {
form.reset();
}
// if you are using custom hidden inputs or not reset by default
modal.querySelectorAll("input[type=text], input[type=email], input[type=number], textarea").forEach(el => {
el.value = "";
});
modal.querySelectorAll("input[type=checkbox], input[type=radio]").forEach(el => {
el.checked = false;
});
modal.querySelectorAll("select").forEach(el => {
el.selectedIndex = 0;
});
});
});
</script>
### β
Explanation
– Uses Bootstrap 5 modal event `show.bs.modal`.
– `form.reset()` clears standard fields.
– Extra clearing code ensures hidden inputs, checkboxes, radios, and selects are also reset.
—
π Do you want me to also reset your **multi-step (Prev/Next) steps** inside this modal (so it always opens at Step 1), or just clear the input values?
<script>
document.addEventListener("DOMContentLoaded", function () {
const modal = document.getElementById("multiStepModal");
const form = modal.querySelector("form"); // global form under modal
const steps = modal.querySelectorAll(".step");
let current = 0;
function showStep(index) {
steps.forEach((s, i) => s.classList.toggle("active", i === index));
}
// NEXT buttons
modal.querySelectorAll(".nextstep").forEach(btn => {
btn.addEventListener("click", function () {
if (current < steps.length - 1) {
current++;
showStep(current);
} else {
form.submit();
}
});
});
// PREV buttons
modal.querySelectorAll(".prevstep").forEach(btn => {
btn.addEventListener("click", function () {
if (current > 0) {
current--;
showStep(current);
}
});
});
// Reset form + steps whenever modal opens
modal.addEventListener("show.bs.modal", function () {
if (form) form.reset(); // clear form values
current = 0; // reset to first step
showStep(current); // show first step
});
// initialize
showStep(current);
});
</script>
### β
What this does:
– Targets your **form only inside `#multiStepModal`**.
– Clears all inputs on modal open (`form.reset()`).
– Resets **step navigation** to step 1 each time modal is shown.
– Keeps Next/Prev working correctly.
—
Do you also want me to **auto-hide Prev button on step 1 and Next button on last step** so the navigation looks cleaner?
function addWorkerModal()
{
document.getElementById('Form_AddWorkerForm').reset();
var modal = new bootstrap.Modal(document.getElementById('multiStepModal'));
modal.show();
}
Hereβs how you can update your function:
<script>
function addWorkerModal() {
// reset form fields
document.getElementById('Form_AddWorkerForm').reset();
// reset step navigation to first step
const modalEl = document.getElementById('multiStepModal');
const steps = modalEl.querySelectorAll(".step");
let current = 0;
steps.forEach((s, i) => s.classList.toggle("active", i === current));
// show modal
var modal = new bootstrap.Modal(modalEl);
modal.show();
}
</script>
### β
What changed:
– After resetting the form, it loops through `.step` elements inside `#multiStepModal` and makes only the **first step active**.
– Ensures every time you call `addWorkerModal()`, the modal starts fresh at step 1.
—
π Do you also want me to handle hiding/showing the **Prev/Next buttons** automatically (so "Prev" is hidden on Step 1 and "Submit" replaces "Next" on the last step)?
So you need to explicitly clear them.
Hereβs the updated `addWorkerModal()` with file input reset included:
<script>
function addWorkerModal() {
// reset form fields
const form = document.getElementById('Form_AddWorkerForm');
form.reset();
// reset file inputs manually
form.querySelectorAll('input[type="file"]').forEach(input => {
input.value = "";
});
// reset step navigation to first step
const modalEl = document.getElementById('multiStepModal');
const steps = modalEl.querySelectorAll(".step");
let current = 0;
steps.forEach((s, i) => s.classList.toggle("active", i === current));
// show modal
var modal = new bootstrap.Modal(modalEl);
modal.show();
}
</script>
### β
What it does:
– `form.reset()` β clears normal fields.
– Loops through all “ inside the form and empties them.
– Ensures modal **always opens on Step 1**.
—
Do you also want me to make sure the **file preview (if youβre showing selected filename or thumbnail)** gets cleared when resetting?