473,506 Members | 16,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking for changes on form (is form dirty)

Claus Mygind
571 Contributor
There is a fairly standard method out there for checking or testing if one or more controls on your form has been changed by simply looping through the form elements and checking their state vs. the default state. ie: the following code

Expand|Select|Wrap|Line Numbers
  1.     this.formIsDirty = function (formObj)
  2.     {
  3.         for (var i = 0; i < formObj.elements.length; i++)
  4.         {
  5.             var element = formObj.elements[i];
  6.             var type = element.type;
  7.             if (type == "checkbox" || type == "radio")
  8.             {
  9.                 if (element.checked != element.defaultChecked)
  10.                 {
  11.                     return true;
  12.                 }
  13.             }
  14.             else if (type == "hidden" || type == "password" || type == "text" ||
  15.                      type == "textarea")
  16.             {
  17.                 if (element.value != element.defaultValue)
  18.                 {
  19.                     return true;
  20.                 }
  21.             }
  22.             else if (type == "select-one" || type == "select-multiple")
  23.             {
  24.                 for (var j = 0; j < element.options.length; j++)
  25.                 {
  26.                     if (element.options[j].selected !=
  27.                         element.options[j].defaultSelected)
  28.                     {
  29.                         return true;
  30.                     }
  31.                 }
  32.             }
  33.         }
  34.         return false;
  35.     };
  36.  
That works great when your form is loaded with a value.

And I assume (if I am wrong here please correct me) that the default value/status is untouchable for updating.

This technique does not lend itself very well to a form that loads blank and is then back filled with data from an ajax call. In this scenario, it would be nice to establish a point at which the default values are set.

There is a difference between loading a form with data from the database and changing one or more of the values which have been loaded.

Is there some similar generic routine that could be used to test if one or more states/values of a form loaded from an AJAX call have been changed?
Aug 25 '11 #1
4 4829
Dormilich
8,658 Recognized Expert Moderator Expert
the only statement in this regard I found is
The accept, alt, max, min, multiple, pattern, placeholder, required, size, src, and step IDL attributes must reflect the respective content attributes of the same name. The dirName IDL attribute must reflect the dirname content attribute. The readOnly IDL attribute must reflect the readonly content attribute. The defaultChecked IDL attribute must reflect the checked content attribute. The defaultValue IDL attribute must reflect the value content attribute.
so I guess it doesn’t work with AJAX-given "default" values.
Aug 26 '11 #2
Claus Mygind
571 Contributor
Yeah! that was the conclusion I also reached. Interesting the number of articles found on the web touting this as the way to go in handling change detection when ajax and json is the main method of communication these days.

Thanks for the followup.
Aug 26 '11 #3
Dormilich
8,658 Recognized Expert Moderator Expert
you can of course define your own property (say input.predefined), where you can write your default value from AJAX.
Aug 26 '11 #4
Claus Mygind
571 Contributor
That is one way to do it. I currently have a clunky system by which I update a global flag when any control is updated on the screen. On navigation from the screen without a save I alert the user to cancel or save.

I might have been using the test of the default value if I had not gotten this advice http://bytes.com/topic/javascript/an...ld#post3128929 back on May 25 '08. From the advice I determined there was no way to detect the default value, which of course was wrong.

Your method is similar to what I am doing, but I see it may be possible to use your concept to write a generic routine that would run when the form is loaded and add the predefined property to each control, which my ajax calls could update. It may be worth writing a small article about that and posting the example of how that might be done as an alternative to the current approach. You should think about that.

Thanks as always for your great advice.
Aug 26 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2716
by: clarket | last post by:
Hi, I have a html form where all the details are saved in a MySQL database that uses PHP to insert the data into the database. The problem I'm having is that if people dont submit the form...
4
18735
by: deko | last post by:
I'm trying to set up a "preferences form" where users can choose from a few different pre-defined colors. The color numbers and form names are saved in tblColors. The below code changes the form...
3
7525
by: fh1996 | last post by:
Form.Owner and Form.Parent, what's the difference between them? Form.ShowDialog() and Form.Show(), when to use which? Form.Activated(), what does it mean when saying a Form is "activated"? ...
4
1128
by: Dino M. Buljubasic | last post by:
I'd like to check if a form is already open before opening it and if it is not, then open it, otherwise just make it the active form. How can I do that? Regards, Dino --
1
1885
by: astro | last post by:
Is there a command/syntax that will force a given datagrid to submit it's changes to it's dataset? I know the currencymanager is responsable for this.......just can't get it to work. I've...
1
1813
by: lesperancer | last post by:
I've got a form that is opened in readonly mode and no fields can be changed, great but if I click on a combo box that has an _enter() event that sets a field on my form to a value (albeit the...
4
2187
by: gazelle04 | last post by:
I'm trying to track data changes on my database. On form I create a procedure on Before Update event to track data changes, here it is: (It also puts the username who changes the record. Dim ctl...
2
1963
by: JonBosker | last post by:
I have a form developed in ASP.net and as such all of my controls are server controls and do not have access to onchange events. What I need is a javascript program to detect changes. Here is...
3
1461
by: The Mad Ape | last post by:
Hi Whenever I open a form and want to check for an existence of the form I use the following code: Dim frm As Form For Each frm In Me.MdiParent.MdiChildren If TypeOf frm Is Form17 Then...
3
4165
by: dirksza2009 | last post by:
Hi, I've made a multi user (4 end users) database in Access 2000. I've made data tables, reference tables etc which sits on a shared drive and I've made individual front ends for the end users...
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.