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?)
508 stars 145 forks source link

New input fields #111

Open albrahimx opened 8 years ago

albrahimx commented 8 years ago

Hello,

Thank you for the code!

I have a problem when applying this code. It works perfectly fine with the fields that is already written in the HTML. But, I have a form that allows the user to add a new input field by using JavaScript. The problem is that it doesn't check the new input fields added by JavaScript.

Is there a way to make this code work with the new added fields?

Many thanks,

nenad commented 8 years ago

Yes, it's included in the documentation:

/*
*  If you're dynamically adding new fields/inputs, and would like to track 
*  their state, trigger Are-You-Sure to rescan the form like this:
*/
$('#my-form').trigger('rescan.areYouSure');
ibmjm commented 7 years ago

As far as I can tell (from experimentation), doing $('#my-form').trigger('rescan.areYouSure'); will reset the "dirty" flag on the form, which isn't necessarily what is wanted.

For example, I might have a checkbox which the user checks (marking the form as dirty), and a button which the user then clicks to add a file-upload control to the form dynamically. If I rescan as suggested, it seems to reset the form to clean.

I am open to being proven wrong but that's what seemed to be happening to me.