Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know that a form input has changed?

Tags:

I have a form generated by <% Ajax.BeginForm() {} %> which contains a lot of inputs and texareas.

When an input value change, I need to know about it and mark the input and the form as "dirty". If the user tries to leave the page without saving, I will ask him or her to confirm abandoning changes.

Any suggestion to how this should be done?

like image 706
tkalve Avatar asked Dec 30 '10 12:12

tkalve


People also ask

How can I tell if form data has been modified?

$("#myform"). trackChanges(); and check if a form has changed: if ($("#myform").

How do you reset inputs on a form?

You can easily reset all form values using the HTML button using <input type=”reset”> attribute. Clicking the reset button restores the form to its original state (the default value) before the user started entering values into the fields, selecting radio buttons, checkboxes, etc.

Can form inputs have the same name?

Yes. More, it is essential if you are dealing with radio button groups.


1 Answers

Resurrecting this old question because I thought of a simpler/better way. Instead of listening to events on the various inputs, you can serialize the initial form data, store it, and then serialize it again later and check if it's changed, like this:

var originalFormData = $('form#formId').serialize(); function checkFormChanged() {     if(originalFormData !== $('form#formId').serialize()) {         //it's dirty!     } } 

One additional advantage here is that if the user makes a change and then reverts it, this check will report the form as clean.

like image 64
SwabTheDeck Avatar answered Sep 21 '22 03:09

SwabTheDeck