Sometimes, we want to clear all fields in a form upon going back with the browser back button with JavaScript.
In this article, we’ll look at how to clear all fields in a form upon going back with the browser back button with JavaScript.
How to clear all fields in a form upon going back with the browser back button with JavaScript?
To clear all fields in a form upon going back with the browser back button with JavaScript, we can listen to the pageshow event.
For instance, we write
window.addEventListener("pageshow", () => {
// update hidden input field
});
to call addEventListener
to listen to the pageshow event.
We update the fields in the event handler which is called when we show the page.
Conclusion
To clear all fields in a form upon going back with the browser back button with JavaScript, we can listen to the pageshow event.