473,657 Members | 2,509 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 4836
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.predefine d), 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
2728
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 then the data doesnt get saved to the database.
4
18749
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 backcolor no problem, but the colors go back to what they were before if I close and reopen the database. How to I save the changes? Shouldn't "DoCmd.RunCommand acCmdSave" do the trick? For Each varF In Array("frm0", "frm1", "frm2", "frm3")...
3
7533
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"? Thanks!
4
1131
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
1888
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 tried; CType(ctrl, DataGrid).BindingContext.Item(0).EndCurrentEdit() To no effect.
1
1819
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 same value it currently has), the form becomes 'dirty' and from that point on, I can changed any data in any field and I checked the .allowEdit setting, and it is still false, which is misleading me ?
4
2200
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 As Control For Each ctl In Controls If TypeOf ctl Is TextBox Then If ctl.Value <> ctl.OldValue Then Dim ans As Integer ans = MsgBox("A record has changed." & vbcrkf + vbCrLf & _
2
1973
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 my idea... when the page loads the program cycles through all of the objects in the form and saves their values into a variable. Then, when the user attempts to navigate away the program cycles through all of the objects in the form and saves the...
3
1466
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 frm.Close() End If
3
4186
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 which gives them specific views of the data. I'd like to track all the changes made to a record which works find with the following code : basAuditTrail: Option Compare Database Const cDQ As String = """" Sub AuditTrail(frm As Form, recordid As...
0
8421
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8518
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.