DirtyForm

Let the fun begin!

Have you ever made changes and then clicked the wrong button or switched to another tab on the page before you submitted the form and then realized that all your changes are gone. Wouldn’t it be nice if you were given a notification before you left so you would know what was about to happen. I thought so. So I wrote a jQuery plugin that does just that.

It’s very easy to integrate with your app as long as you’re using jQuery (If you’re not you should be).

How do you use it?

Just specify whatever forms you want to be watched as follows.


$(document).ready(function($){
$('form').dirty_form();
...
});

And then specify the links that should stop progress when a form has unsaved changes.


$(document).ready(function($){
...
$('a').dirty_stopper();
});

And that’s all there is to it. Then when you change an input on the form and try to click one of those links a pop-up will display asking if you would like to proceed or go back and save your changes. This can save your users a lot of frustration and wasted time.

If you have the livequery jQuery plugin installed DirtyForm will also work for form elements added at any point after the page loads.

In the near future I’m going to try to make this plugin work with the jQuery UI tabs function. It can be downloaded from github here: DirtyForm

Also posted on my blog at: puts post

4 Comments

  1. Posted January 9, 2009 at 3:50 am | Permalink

    Solid! I need this for the blog posting ;-)

  2. Posted January 9, 2009 at 8:08 am | Permalink

    Just to be clear, by “…or switched to another tab” you’re talking about jQuery UI “tabs” right, not browser tabs?

  3. Posted January 9, 2009 at 4:26 pm | Permalink

    @cary: Glad I could help! :)

    @dvd: Yeah, some sort of javascripty tabs on a page. The plugin doesn’t work with them yet but it won’t be long now.

  4. Posted June 30, 2009 at 12:43 pm | Permalink

    excellent bit of code. v.useful.

    the only extra feature I needed was the ability to exclude certain items from the test,
    so I changed as follows:

    - $(‘:input:not(:hidden,:submit,:password,:button)’, dirtyform).each(function(){
    + $(‘:input:not(:hidden,:submit,.dirtyignore,:password,:button)’, dirtyform).each(function(){

    that way I can do class=’textfield dirtyignore’ and it is, well ignored.

    Thanks again
    –Richard
    http://zaretto.com

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">