codedance / jquery.AreYouSure

A light-weight jQuery "dirty forms" Plugin - it monitors html forms and alerts users to unsaved changes if they attempt to close the browser or navigate away from the page. (Are you sure?)
507 stars 145 forks source link

Fix rescan bug and implement getDirtyFields #131

Open AlejandroFrias opened 7 years ago

AlejandroFrias commented 7 years ago

@codedance

Changes

  1. Fixing small bug in resan.
  2. Exposing getDirtyFields and isDirtyField as available triggers

Rescan Bug

Rescan shouldn't be recalculating the original value when the original value is actually an empty string.

My use case that came across this (IMO) bug: Dynamic form. Add a new blank field to be filled out. Run rescan, which correctly sets the original value of the field to empty string. Then fill in the field, yay it's dirty. Then I add another field. This time rescan incorrectly changes that first field's original value to what I've typed so. Oh no, form suddenly not dirty anymore.

Exposing getDirtyFields

Also needed some functionality to keep my code cleaner. looks like this was asked for in issue https://github.com/codedance/jquery.AreYouSure/issues/74. So here's my take on that, too.

You can now get the dirty fields:

$("#my-form").trigger("getDirtyFIelds.areYouSure").data("dirtyFields");

Just adds a data value dirtyFIelds in which the array of dirty fields is added.

DIntriglia commented 3 years ago

I know this is an old item, but I have confirmed that this implementation works perfectly, and should be merged if possible. I also modified it a bit to add support for a input dirty field class, so I can easily style fields that are dirty and identify which fields are causing the form to be dirty right with jQuery. Thanks @AlejandroFrias