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

How to forcibly mark the form as dirty #134

Open TNBobbie opened 6 years ago

TNBobbie commented 6 years ago

Hello,

I have a form loaded which is clean. Now, due to certain conditions, up on clicking a button, the entire page reloads with some additional text boxes on the page. At this point, is there a way to treat it as dirty & raise the popup?

I tried using the following, but that doesn't seem to do the trick. $('#aspnetForm').trigger('checkform.areYouSure'); $('#aspnetForm').trigger('rescan.areYouSure');

Thanks Bobby

jamgregory commented 4 years ago

I expect you might not need this now @TNBobbie (it is over 3 years late!) but just in case anyone else comes across this issue, you can do something like this:

setTimeout(function(){
  $('#aspnetForm').toggleClass('dirty', true);
}, 10);

I chose to wrap it in a setTimeout just in case areYouSure wiped out the dirty class before we got round to setting it (it doesn't work if you initialise the form with the dirty class set, for example).