Showing posts with label forms. Show all posts
Showing posts with label forms. Show all posts

Tuesday, January 13, 2009

DirtyForm: Watching for form changes with jQuery

Last time I mentioned that I had just finished the first steps of a jQuery plugin that would watch your forms and notify you before you left the page so your users don't lose that input they just took the time to type out but forgot to save. Well now its a little more robust and flexible.

What's new?


Lots of stuff has been updated.

  • Dirty and Clean events are fired off when a form has unsaved changes, and again when the form is cleaned again.

  • It works with jQuery tabs now (they are tricky and don't work like normal links).

  • Not limited to forms. Any container of inputs will work just fine.


Dirty/Clean Events


Forms can observe the "dirty" event for customized behavior. So you could set up the dirty form and then bind a function to the dirty event like so:

$(function(){
$("form")
.dirty_form()
.dirty(function(event, data){
var label = $(event.target).parents("li").find("label");
$("body").append("

" + label.text() + "Changed from " + data.from + " to: " + data.to+ "

")
})
});

Dirty Forms + jQuery-UI tabs


You can use tabs with DirtyForm easily. Just be sure that you make the tabs into stopper links after they are set up as tabs so they can be recognized as such and set up properly. They need a lot of special casing.

$(function(){
$("#nav").tabs();
$("#nav a").dirty_stopper();
});

If you've already set up all the links for the app as stoppers don't worry this will still work as the other events will be unbound. No penalty for making any links into a stopper twice!

Not just for forms!


If you like throwing inputs in your table rows DirtyForm can handle it. Just tell it where you want it to look.

$(function(){
$('tr').dirty_form();
});

The plugin is available for download from github: DirtyForm