0

Is there anyway to check any of the HTML components (i.e input elements) value in a form got changed so that i can pop up a alert saying "would like to save changes?" before navigating to other page.

Thanks.

2 Answers 2

1

Since you have jQuery in your question:

After page load, use $.serialize to create a snapshot of the form.

On submit, run serialize again.

Then you can use use (or create) an isEqual function to compare if there has been a change.

This question might help you on that last step: How to determine equality for two JavaScript objects?

EDIT

jQuery also has a serializeArray method which may make that process easier.

https://api.jquery.com/serializeArray/

Sign up to request clarification or add additional context in comments.

Comments

0

jQuery('.form_component').change(function (){..}); , where form_component should be a class common for all your input elements in the form.

2 Comments

Agreed!. But i would like to cover this scenario as well. Lets say if a user changes the value from 0 to 1 and then changes back to 0. Ideally, in this case nothing has been changed. Is there a way to cover as this well
If you're looking to show an alert only before the user navigates away, store the form values in an array. Check if the new values is equal to the array stored. If not, show an alert. Voila!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.