3

I'm using this: $('form').dirtyForms(); from https://github.com/snikch/jquery.dirtyforms to check if my form is dirty. However, on my page I have some dropdown's that are simply used for filtering (they should not make my form "dirty"). Right now when I select any of these drop down's it causes my form to become dirty. Using jquery.dirtyforms (I read their docs but do not see how), how do I exclude selectors (dropdowns, textboxes, etc.) maybe via a class name so that they do not mark the form as dirty.

I tried various things like assigning these dropdowns / filters a class called ignoreDirty then in my jquery I did this:

$('form').dirtyForms().ignoreClass('ignoreDirty');

This produces an error, so I must be doing something wrong. Note I've also tried setting it via property:

$('form').dirtyForms({ ignoreClass : "ignoreDirty" });

But this still makes my form dirty for any control whose class name is still ignoreDirty Please note these filters cause postbacks but lets say I go to my form and have not made a single change. I start clicking on these filters and the minute they post back this happens:

enter image description here

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
JonH
  • 32,732
  • 12
  • 87
  • 145
  • Try with `$('form').dirtyForms({ ignoreClass : "ignoreDirty" });` – Satpal May 15 '14 at 13:15
  • I tried that in the past but it still causes my form to become dirty. I get the popup (after a postback) that the form has been changed and that I need to save my changes. – JonH May 15 '14 at 13:19
  • seems like a bug in the library – Ejaz May 15 '14 at 13:33
  • Is there any way to ignore a selector? Or is there any way around this, @Ejay - did you test this on your end and confirm? – JonH May 15 '14 at 13:34
  • yes, tested and it does what you're experiencing – Ejaz May 15 '14 at 13:38
  • @Ejay I posted as an issue here: https://github.com/snikch/jquery.dirtyforms/issues/63 but this one scares me: https://github.com/snikch/jquery.dirtyforms/issues/56 does this mean the ignoreClass only works for link type elements? – JonH May 15 '14 at 13:54

2 Answers2

2

What can one say, the plugin code makes almost no sense to me :D However to make it quickly work for ignoring select boxes, you could replace its onSelectionChange with following

Original function

var onSelectionChange = function() {
    $(this).dirtyForms('setDirty');
}

New version

var onSelectionChange = function () {

   //this is the new line. self explanatory
   if ($(this).hasClass($.DirtyForms.ignoreClass)) return;


   $(this).dirtyForms('setDirty');
}

After this you should rely on the original developer for a proper fix. I just posted this as an answer because of space in comments

Ejaz
  • 8,719
  • 3
  • 34
  • 49
  • is this within the jquery.dirtyforms js file? – JonH May 15 '14 at 14:18
  • yes, please also make sure that your original function looks like the one I've included. I downloaded the code from github repo your linked to, but just in case :D – Ejaz May 15 '14 at 14:19
  • I dont think this will work, because there are some drop downs that I want to make my form dirty. But my filter drop downs shouldnt make the form dirty... Do you see what I mean? – JonH May 15 '14 at 14:22
  • Maybe it should be `if ($(this).hasClass("ignoreDirty")) return;` where ignoreDirty is the class we can use to specify which controls should not cause a form to become dirty? – JonH May 15 '14 at 14:24
  • This doesn't it ignore all dropdowns. The ones with ignore class will be ignored. also, this method worked when I set ignoreClass is set using `$.DirtyForms.ignoreClass="ignoreDirty"`. – Ejaz May 15 '14 at 14:26
  • I placed this: `$.DirtyForms.ignoreClass = "ignoreDirty";` in my `document.ready()`. I added your code, but when I make a change to any drop down list even ones that I believe should make the form dirty it doesnt work, it never comes up with the popup to save changes. In fact, even regular input type boxes now never make the form dirty. – JonH May 15 '14 at 14:30
  • damn, I tried to help :D Wait, "Save changes"? I doubt if this plugin is for that purpose. I think it only prevents the page from reloading/redirecting – Ejaz May 15 '14 at 14:34
  • BTW `$(function () { $.DirtyForms.ignoreClass = 'nodirty'; $('form#df').dirtyForms({}); });` using this code I'm seeing all expected behaviors, i.e., input boxes making form dirty, normal ` – Ejaz May 15 '14 at 14:37
  • Hmm somehow this is now working....I will continue to test but I believe this is good! Thank you – JonH May 15 '14 at 14:45
0

There seems to be 2 different issues here.

First of all, you are attempting to set the ignoreClass to ignoredirty. ignoredirty is the default value, so there is no reason to set it. However, if you do need to set it to something else, you can do so using the syntax:

$.DirtyForms.ignoreClass = 'my-ignore-class';

Secondly, in version 1.0.0 the ignoreClass only worked on Hyperlinks. This behavior has been amended to work with input and selection elements in version 1.1.0.

In version 1.2.0, you can now also set the ignoreClass to parent container elements to ignore input or clicks from any element within.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • 1
    I know this is an old question, but in case anyone is looking at this (as I was), the default ignore class is 'dirtyignore' not 'ignoredirty'. Perhaps this has changed in the last four years. See @NightOwl88's page [link](https://devhub.io/repos/NightOwl888-jquery.dirtyforms) – Michael Dec 06 '18 at 16:19