Giter VIP home page Giter VIP logo

dirty_form's Introduction

=====================
= DirtyForms How-To =
=====================

Example usage:

All forms in the page will be observed
Whenever an input value changes it will get the 'changed' class added to it
  $(function(){
    $("form").dirty_form();
  });
    
  // All forms in the page will be observed
  // Whenever an input value changes it will get the 'forever_changes' class added to it
  $(function(){
    $("form").dirty_form({changeClass: "forever_changes"});
  });
    
Often times you want to apply the "changed" CSS class to more than one element.
To support this, DirtyForm can take a "addClassOn" option.
Pass in a function that will be executed in the context of the dirty input element
and the return value of the function will be added to the list of elements that
get the "changed" class. In this function "this" refers to the dirty element.
The code below will add the "change" class to the dirty input and to any labels 
descendants of the li element that contains the input. 
Example:
  $("form").dirty_form({
    addClassOn: function(){ 
      return this.parents("li").find("label");
      }
    });
  
Forms within the "ch-ch-ch-changes" DIV in the page will be observed
Whenever an input value changes it will get the 'changed' class added to it
  $(function(){
    $("#ch-ch-ch-changes form").dirty_form();
  });

  // Forms can observe the "dirty" event for customized behavior
  $(function(){
    $("form")
      .dirty_form()
      .dirty(function(event, data){
        var label = $(event.target).parents("li").find("label");
        $("body").append("<p>" + label.text() + "Changed from " + data.from + " to: " + data.to+ "</p>")
      })
  });
  
As of this writing jQuery does not support event bubbling for custom events
See discussion here: http://groups.google.com/group/jquery-dev/browse_thread/thread/1acd358ceeacd67a
Once the patch is in any DOM element can subscribe to the "dirty" event

By default, hidden inputs are not observed; this can be overridden by
setting the includeHidden configuration:

$.DirtyForm.includeHidden = true;

Dirty form detection is performed using the blur event; when any input
loses focus, its value is checked against its previous value and if
they differ the field is marked dirty.  The downside of this is that
changes to radio buttons and checkboxes won't be detected until the
element loses focus, instead of immediately when checked.  You can
tell DirtyForm to use a different event (i.e. change) with the
monitorEvent property:

$.DirtyForm.monitorEvent = 'change';
  
The $.DirtyForm singleton can be used for whole-page configuration
  $(function(){
    $.DirtyForm.dynamic = false // Don't bother watching out for dynamic additions to the DOM
    $.DirtyForm.debug = true    // Turn on logging
    $.DirtyForm.logger = my_fancy_logger_fn // Override the default logger from console.log (if Firebug is available) or a plain-jane alert (for IE) -- NOT WORKING RIGHT NOW. GRR
  });
  
All configuration options can be set per-instance
  $(function(){
    $("#some_form").dirty_form({dynamic: false})
    $("#other_form").dirty_form({dynamic: true})
    $("#third_form").dirty_form({debug: true})
  });

dirty_form's People

Contributors

acvwilson avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.