Say I wish to run a popup if a certain form was submitted already, but I want to do that outside a submit event handler from another JS file such as popups.js.
Is there a way to test if a specific form has been submitted already for the user, which I could use in popups.js?
Hi @bendqh1, you mean it has been submitted regularly (with no default prevented) and you need to check that on the next page load? In this case you might set a data-submitted attribute (say) on the back end serving the form, or maybe set a cookie with submitted form ID.
Or on the client side alone, store the submitted form ID in the session storage from your other script (if submit event listeners are allowed here):
I didn’t know that in this case it matters if the form’s default behavior was prevented or not but I normally do prevent default behavior for forms.
May you recommend to act differently in case default behavior is prevented?
Well then you need to go with the session storage solution, or set a data attribute using JS (but this won’t persist if the user reloads the page or otherwise navigates your site)…
window.addEventListener('submit', event => {
// This would probably better be done where
// the default is getting prevented, so you
// might check if the form got processed
// successfully (validation, AJAX etc.)
event.target.dataset.submitted = true
})
popup.js
const myForm = document.getElementById('my-form')
if (myForm.dataset.submitted) {
// Do stuff
}