a little dirty talk

Yesterday, we received some final comments regarding one of my projects, which resulted in some minor tweaks, easy enough to make. One of the suggestions was a pop-up box that told the user to be sure to click the submit button after updating notes, especially since there are several other forms on the page that could be clicked, and not result in updated notes. I was thinking about it, and decided it was going to be a pain to do, when jQuery came to the rescue.

In programming terms, when a form field is updated or changed in some way, that field is then considered ‘dirty’ (and here you thought I was going to talk about something more fun). So what I had to accomplish in JavaScript was to have a way to determine that the field was dirty, and then on any and all submit buttons (except the update notes) I would have to throw a function to pop up my cute little box with explanation. After playing around, I finally settled on a very simplistic set of functions.

I planned to use the onChange event to determine that a field has become dirty, though on the jQuery version it’s just ‘.change’. Since this function is supposedly fired whenever the changed field loses focus, I figured that it’d be the quick and easy way to get the job done. So I created a jQuery selector field of .checkDirty and added it to all fields that I wanted to keep a dirty eye on. So in my document ready function, I put this line: $(‘.checkDirty’).change(function(){isDirty=true;}); and set up a global variable isDirty=false. Yes, this method is quick and dirty.

My next problem was the submit buttons. Another jquery selector to the rescue.
$(“:submit”).click(function () {
return checkDirty(this);
});

I throw this little function within the change function and it will get fired after every dirty little change. Within the checkDirty function I ‘return true’ to keep the form process going if the form that’s being submitted is the update notes form. It would be rather silly to have the pop-up on that form. I also make sure the fields are actually dirty, with my isDirty flag. So if it passes all of the tests, I pop up my .dialog with the little message. I also set a continue flag here so that we don’t get stuck in a loop every time they click submit, I just want the warning to go up once.