-
Notifications
You must be signed in to change notification settings - Fork 2
#144 confirm if unsaved form changes #169
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| const forms = document.getElementsByClassName('confirmIfUnsavedChanges'); | ||
|
||
| let hasUnsavedChanges = false; | ||
|
|
||
| for(const form of forms) { | ||
| const inputs = Array.prototype.slice.apply(form.getElementsByTagName('input')); | ||
| const textareas = Array.prototype.slice.apply(form.getElementsByTagName('textarea')); | ||
| const selects = Array.prototype.slice.apply(form.getElementsByTagName('select')); | ||
|
||
| const elements = inputs.concat(textareas).concat(selects); | ||
| console.log(elements) | ||
tjfoerster marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for(const elem of elements) { | ||
| elem.addEventListener('input', () => {if(!hasUnsavedChanges) hasUnsavedChanges = true}); | ||
| } | ||
| form.addEventListener('submit', () => {if(hasUnsavedChanges) hasUnsavedChanges = false}); | ||
| } | ||
|
|
||
| window.addEventListener('beforeunload', (e) => { | ||
| if (hasUnsavedChanges) { | ||
| (e || window.event).returnValue = ''; | ||
|
||
| return ''; | ||
| } | ||
| }) | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ | |
| </div> | ||
| [% END %] | ||
|
|
||
| <form method="post" action="[% update_url %]"> | ||
| <form class="confirmIfUnsavedChanges" method="post" action="[% update_url %]"> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked out your branch and tested. The JS file was loaded but it had no effect. I made changes and could leave the page without warning. |
||
|
|
||
| <div class="row"> | ||
| <div class="col-sm-12 py-3"> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like your generic implementation with the
<form>class. Do you think we should simply add this to our globalscript.jsto utilize it on any page? 🤔