Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic way to detect if html form is edited

I have a tabbed html form. Upon navigating from one tab to the other, the current tab's data is persisted (on the DB) even if there is no change to the data.

I would like to make the persistence call only if the form is edited. The form can contain any kind of control. Dirtying the form need not be by typing some text but choosing a date in a calendar control would also qualify.

One way to achieve this would be to display the form in read-only mode by default and have an 'Edit' button and if the user clicks the edit button then the call to DB is made (once again, irrespective of whether data is modified. This is a better improvement to what is currently existing).

I would like to know how to write a generic javascript function that would check if any of the controls value has been modified ?

like image 359
Sathya Avatar asked Jun 06 '09 13:06

Sathya


People also ask

How do you check that an HTML form has been changed?

This page demonstrates FormChanges(string FormID | DOMelement FormNode), a generic, standalone JavaScript function which detects when the user has made any updates to a form. It returns an array of changed elements – an empty array indicates that no changes have been made.

What method can you use to check if form data has been changed?

skills. selectedIndex); if (c) alert("#skills has changed"); That's how you check whether any form element has changed.

How do I check if a HTML form is valid?

Using the email type, we can check the validity of the form field with a javascript function called… checkValidity() . This function returns a true|false value. checkValidity() will look at the input type as well as if the required attribute was set and any pattern="" tag .

Which is a HTML form that remembers how you filled it out?

A sticky form is simply a standard HTML form that remembers how you filled it out.


2 Answers

In pure javascript, this would not be an easy task, but jQuery makes it very easy to do:

$("#myform :input").change(function() {    $("#myform").data("changed",true); }); 

Then before saving, you can check if it was changed:

if ($("#myform").data("changed")) {    // submit the form } 

In the example above, the form has an id equal to "myform".

If you need this in many forms, you can easily turn it into a plugin:

$.fn.extend({  trackChanges: function() {    $(":input",this).change(function() {       $(this.form).data("changed", true);    });  }  ,  isChanged: function() {     return this.data("changed");   } }); 

Then you can simply say:

$("#myform").trackChanges(); 

and check if a form has changed:

if ($("#myform").isChanged()) {    // ... } 
like image 97
Philippe Leybaert Avatar answered Oct 23 '22 04:10

Philippe Leybaert


In case JQuery is out of the question. A quick search on Google found Javascript implementations of MD5 and SHA1 hash algorithms. If you wanted, you could concatenate all form inputs and hash them, then store that value in memory. When the user is done. Concatenate all the values and hash again. Compare the 2 hashes. If they are the same, the user did not change any form fields. If they are different, something has been edited, and you need to call your persistence code.

like image 23
Matthew Vines Avatar answered Oct 23 '22 02:10

Matthew Vines