Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if html form has changed

Using jquery I've added a change handler to a form. This works when any input is changed BUT only if the user manually changes an input and not when some other code changes the input.

Is there any way to detect if a form has changed even if its inputs are changed by code?

like image 495
kingsaul Avatar asked Apr 25 '12 08:04

kingsaul


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.

Where is HTML form data stored?

The form data can be stored at a email account in the admin page or,at a database like MySQL data base. The email account is placed in a web server, which provides a password to access it.

Are HTML forms still used?

Form tags are still necessary if you want to be able to submit the form without AJAX (which may be useful in the early stages of development).

What happens when HTML form is submitted?

Most HTML forms have a submit button at the bottom of the form. Once all of the fields in the form have been filled in, the user clicks on the submit button to record the form data. The standard behaviour is to gather all of the data that were entered into the form and send it to another program to be processed.


2 Answers

Yes, there seems to be some confusion over this. In an ideal world you would expect the onchange event to happen whenever the inputs change but thats not what happens. I'm sure for good reasons to - maybe not.

One way I've overcome this obstacle is to capture the form state into a variable just after displaying it and then just before submitting it to check if the state has changed and to act accordingly.

An easy state to store is what the serialize function returns. An easy place to store the state is using the data functionality. Both serialize and data are available with jquery.

Of course you can use other different forms of state (some form of hash) or storage for this state (standard global variable for example).

Here is some prototype code:

If your form id is 'xform' then you can call the following code when the form has displayed:

$('#xform').data('serialize',$('#xform').serialize());

And then, when you need to check, for example just before a button submit you can use:

if($('#xform').serialize()!=$('#xform').data('serialize')){
    // Form has changed!!!
}

You could wrap all this up into a copy & paste javascript snippet that will give you a formHasChanged() function to call wherever you need it (NOT TESTED):

$(function() {
    $('#xform').data('serialize',$('#xform').serialize());
});
function formHasChanged(){
    if($('#xform').serialize()!=$('#xform').data('serialize')){
        return(true);
    }
    return(false);
}

But I'll stop here otherwise I'll create yet another jquery plugin.

like image 69
zaf Avatar answered Sep 30 '22 18:09

zaf


Serializing the form is certainly an option, but it will not work if:

  • you want to know which fields have changed
  • it only needs to check a subset of the fields
  • dynamically adding or removing fields.

Fortunately, every form element has a default value associated with its object:

  • input, textarea : defaultValue
  • checkbox, radio : defaultChecked
  • select: defaultSelected

for ex: to ckeck if input or textarea has changed:

var changed = false;
$(":text,textarea").each(function(){
    changed = this.value != this.defaultValue;
    return !changed; // return if at least one control has changed value
});
like image 42
Corneliu Avatar answered Sep 30 '22 19:09

Corneliu