jQuery State form or simple form control

    Good time% username%!

    I want to introduce one interesting plugin for monitoring the state of the form to the public.
    What is a “state of form"? This is a cast of the values ​​that were set in the form at the time of its initialization. There is nothing superfluous in the plugin, no “preference and harlots”, only what it was developed for, namely control over the change of values ​​in the form. What is it for? Well, there are all sorts of situations, for example, for logging changes that have been made to the data. You can try to control the event of leaving the page, suddenly, the user entered something, but forgot to save?

    Who cares , you can play around with jsfiddle .

    The plugin itself is extremely simple. Initialization:
    $(document).ready(function() {
        $('form').state_form();
    });
    

    or advanced option:
    $(document).ready(function() {
        $('form').state_form({
            //name input in form
            inputName: 'changed_state',  //is default
            //add input with changes in form
            insertInForm: 1, //is defaul
            //function before form submit
            //call if form has changes
            ifChanged: function() { //is default
                return true;
            },
            //field for excluded
            exclude: ['field1', 'field2']
        });
    });
    


    Check for state changes:
    $('form').state_form('is_changed');
    

    Get changed fields:
    $('form').state_form('get_changes');
    


    A handler is hung on the form submission event, which before that collects all the changes, encodes them in json, creates a field in the form where it inserts the resulting string and the changes are sent to the server. This feature can be disabled.

    UPD: updated the plugin to 0.0.3, where he added methods for working with the state history. Thanks to RubaXa for your suggestions and criticism!

    Source code github.com/Slavenin/state_form
    I would be glad to comments and suggestions!
    Thank you for attention!

    Also popular now: