473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to check that a form has been changed

Hi everyone,

How do you detect that a form element has been changed? This thread:

http://groups.google.com/group/comp....5a82c9be127790

suggests that you attach onChange event handlers to every form element,
and when the handler fires you update a global 'isChanged' variable.
This technique seems to be a bit messy to me (I have many form
elements), I was thinking about storing the original form object in a
javascript variable when the body loads (in the bodie's onLoad event
handler), and upons submission, doing a javascript equality comparison
with the current form object. If nothing has changed, then the two
objects should be equal, right?

if(oldObject == document.getElementById('form'))
{
alert('has changed!');
}

Would this work?

Cheers

Taras

Apr 30 '06 #1
19 8244
Taras_96 wrote on 30 apr 2006 in comp.lang.javascript:
How do you detect that a form element has been changed? This thread: [..] if(oldObject == document.getElementById('form'))
never ID a form by "form", btw
{
alert('has changed!');
}

Would this work?


Did you check?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 30 '06 #2
"Evertjan." <ex**************@interxnl.net> writes:
never ID a form by "form", btw


Can you show a case where it fails? I can't see any problem with it.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 30 '06 #3
Lasse Reichstein Nielsen said the following on 4/30/2006 5:43 AM:
"Evertjan." <ex**************@interxnl.net> writes:
never ID a form by "form", btw


Can you show a case where it fails? I can't see any problem with it.


It's one of those "Best Practices" things. Easy to confuse document.form
and document.forms

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 30 '06 #4
Taras_96 wrote:
Hi everyone,

How do you detect that a form element has been changed?
It depends on what you mean by 'changed'. Do you mean if any control
has a value that differs from its default value? Or does 'changed'
include where a user changes to some other value, then back to the default?

This thread:

http://groups.google.com/group/comp....5a82c9be127790

suggests that you attach onChange event handlers to every form element,
and when the handler fires you update a global 'isChanged' variable.
An alternative is to go through all the form controls and test if their
current value is the same as their default value. Where it isn't,
they've been changed.

How you do the test depends on the type of element. Inputs of type
"text", "file" or "password" have a defaultValue attribute that is their
default value, so you can test their current value against that.

For select elements, test if the selected option's 'defaultSelected'
attribute is 'true'. For radio buttons and checkboxes, test their
'defaultChecked' attribute.

Another scheme is to always have the first control of any group selected
(select, radios), no checkboxes selected and no value in text controls.
Then you can test against logic rather than values.

Another option is to use onload to create an object that stores the
names or ids of form controls and their values, then you can check
against that when the form is submitted.

This technique seems to be a bit messy to me (I have many form
elements), I was thinking about storing the original form object in a
javascript variable when the body loads (in the bodie's onLoad event
handler),
How to you expect to achieve that?

and upons submission, doing a javascript equality comparison
with the current form object. If nothing has changed, then the two
objects should be equal, right?
In JavaScript, objects are only equal to themselves (I wish I had a
reference for that statement but I don't). There is no way to copy an
objectA to create a new objectB so that :

objectA == objectB

returns true; it will always return false. The statement will only be
true if objectA and objectB are references to the same object.

if(oldObject == document.getElementById('form'))
{
alert('has changed!');
}

Would this work?


No - that is, it will not reliably test whether the form has been
changed or not.
--
Rob
Apr 30 '06 #5
Taras_96 wrote:
How do you detect that a form element has been changed? This thread:

http://groups.google.com/group/comp....5a82c9be127790
suggests that you attach onChange event handlers to every form element,
and when the handler fires you update a global 'isChanged' variable.
This technique seems to be a bit messy to me (I have many form
elements), [...]


If we lived a perfect world, that would not be necessary. The `change'
event is specified to bubble by default, so you can handle that event on
the `form' element or any other parent element (even though it does not
have the corresponding attribute).

<URL:http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-htmlevents>

Try

document.forms[0].onchange = function() { alert(42); };

or

document.forms[0].addEventListener(
'change', function() { alert(42); }, false);

in Geckos, Operas and KHTML-based browsers. When you change a form control
(of the first form in the document, and focus another element after that),
you should see a message box showing "42".

However unfortunately, there is also IE, where it does not bubble, and where
therefore you cannot handle the event easily:

<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/events/onchange.asp>
PointedEars, with a fitting random signature
--
Bill Gates isn't the devil -- Satan made sure hell
_worked_ before he opened it to the damned ...
Apr 30 '06 #6
RobG wrote:
An alternative is to go through all the form controls and test if
their current value is the same as their default value. Where it
isn't, they've been changed.
How you do the test depends on the type of element. Inputs of type
"text", "file" or "password" have a defaultValue attribute that is
their default value, so you can test their current value against that.


This is the approach taken in my functions at
http://www.mattkruse.com/javascript/...ns/source.html

I provide an isChanged() function that takes a reference to any input
control and returns true or false. It relies on generalized getInputValue()
and getInputDefaultValue() functions which work for any input type and are
also useful on their own.

There is also isFormModified() which loops through all form controls and
calls isChanged() on each and also provides a few additional features.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Apr 30 '06 #7
Matt Kruse said the following on 4/30/2006 5:11 PM:
RobG wrote:
An alternative is to go through all the form controls and test if
their current value is the same as their default value. Where it
isn't, they've been changed.
Depending on your definition of "changed", that may not be entirely true
though. The simplest example would be a Select element. User chooses an
option, changes his mind, changes it back to the original choice. Was it
changed?
How you do the test depends on the type of element. Inputs of type
"text", "file" or "password" have a defaultValue attribute that is
their default value, so you can test their current value against that.


This is the approach taken in my functions at
http://www.mattkruse.com/javascript/...ns/source.html

I provide an isChanged() function that takes a reference to any input
control and returns true or false. It relies on generalized getInputValue()
and getInputDefaultValue() functions which work for any input type and are
also useful on their own.
There is also isFormModified() which loops through all form controls and
calls isChanged() on each and also provides a few additional features.


You may want to rethink it depending on what "changed" is supposed to
mean. Whether it has been changed or its value has been changed.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 1 '06 #8
Randy Webb wrote:
Depending on your definition of "changed", that may not be entirely
true though. The simplest example would be a Select element. User
chooses an option, changes his mind, changes it back to the original
choice. Was it changed?


I guess you could play with the meaning of "changed". But in most cases, and
what I coded for, I think the developer is wondering if the form will submit
anything different than it would have when it was first loaded. To check if
any changes need to be saved to a database, for example, before navigating
away from a screen.

There is no way to detect at form-submittal time if a user changed a select
element, then changed it back. If you want that kind of fine-level control,
then you need to look at the onChange of each element and set some global
flag. But I don't see the value in this on a global form level. I can only
think of realistic cases where you'd want to handle it on a per-field basis.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
May 1 '06 #9
Matt Kruse wrote on 01 mei 2006 in comp.lang.javascript:
Randy Webb wrote:
Depending on your definition of "changed", that may not be entirely
true though. The simplest example would be a Select element. User
chooses an option, changes his mind, changes it back to the original
choice. Was it changed?


I guess you could play with the meaning of "changed". But in most
cases, and what I coded for, I think the developer is wondering if the
form will submit anything different than it would have when it was
first loaded. To check if any changes need to be saved to a database,
for example, before navigating away from a screen.

There is no way to detect at form-submittal time if a user changed a
select element, then changed it back. If you want that kind of
fine-level control, then you need to look at the onChange of each
element and set some global flag. But I don't see the value in this on
a global form level. I can only think of realistic cases where you'd
want to handle it on a per-field basis.


Won't this do [on a perfield loop onSubmit()]?

if (myInput.value!=myInput.defaultValue) ...
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 1 '06 #10
Matt Kruse said the following on 4/30/2006 11:16 PM:
Randy Webb wrote:
Depending on your definition of "changed", that may not be entirely
true though. The simplest example would be a Select element. User
chooses an option, changes his mind, changes it back to the original
choice. Was it changed?
I guess you could play with the meaning of "changed". But in most cases, and
what I coded for, I think the developer is wondering if the form will submit
anything different than it would have when it was first loaded. To check if
any changes need to be saved to a database, for example, before navigating
away from a screen.


Very true.
There is no way to detect at form-submittal time if a user changed a select
element, then changed it back.
Sure there is, you use the onchange of the select element to change a
global variable.
If you want that kind of fine-level control, then you need to look at the
onChange of each element and set some global flag.
Yes, that is the only way.
But I don't see the value in this on a global form level. I can only
think of realistic cases where you'd want to handle it on a per-field basis.


I don't see any value in it either. It was more of a mental exercise
than anything else :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 1 '06 #11
Evertjan. wrote:
Won't this do [on a perfield loop onSubmit()]?
if (myInput.value!=myInput.defaultValue) ...


No, because that won't handle checkboxes, select lists, or radio button
groups.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
May 1 '06 #12
Matt Kruse wrote on 01 mei 2006 in comp.lang.javascript:
Evertjan. wrote:
Won't this do [on a perfield loop onSubmit()]?
if (myInput.value!=myInput.defaultValue) ...


No, because that won't handle checkboxes, select lists, or radio button
groups.


Forgetting getElementById() for the moment, Matt,
one could set such default "by hand",
easily done by serverside code:
<input id=c1 type=checkbox defaultChecked=true checked>

<button
onclick='alert((c1.checked==c1.defaultChecked)?"sa me":"changed")'>
test 1</button>

<br><br>

<input id=c2 type=checkbox defaultChecked=false>

<button
onclick='alert((c2.checked==c2.defaultChecked)?"sa me":"changed")'>
test 2</button>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 1 '06 #13
Evertjan. wrote:
Forgetting getElementById() for the moment, Matt,
one could set such default "by hand",
easily done by serverside code:
<input id=c1 type=checkbox defaultChecked=true checked>


I don't get the point.
The element still doesn't have a "defaultValue" property which you said
could be used generically for all elements.

There is no need to set the defaultChecked value manually anyway - its value
is set by the browser. Why would you need to add an (invalid) attribute into
the html?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
May 1 '06 #14
Matt Kruse wrote on 01 mei 2006 in comp.lang.javascript:
Evertjan. wrote:
Forgetting getElementById() for the moment, Matt,
one could set such default "by hand",
easily done by serverside code:
<input id=c1 type=checkbox defaultChecked=true checked>
I don't get the point.
The element still doesn't have a "defaultValue" property which you
said could be used generically for all elements.


I didn't "say", but asked, and you said "no", I thought.
There is no need to set the defaultChecked value manually anyway - its
value is set by the browser. Why would you need to add an (invalid)
attribute into the html?


"defaultChecked" exists?
I did not know that, so what is the problem?

That they should be detected seperately?

That does not seem to be a problem, when you ID the different types of
<input> cleverly.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 1 '06 #15
Evertjan. wrote:
"defaultChecked" exists?
I did not know that, so what is the problem?
Maybe there is a miscommunication, here :)
That they should be detected seperately?
That does not seem to be a problem, when you ID the different types of
<input> cleverly.
That is exactly what my code does - identify each type, and use the
appropriate method to determine if it has changed, using defaultValue,
defaultSelected, etc.

Your post earlier:Won't this do [on a perfield loop onSubmit()]?
if (myInput.value!=myInput.defaultValue) ...


was what I was responding to originally, pointing out that "defaultValue"
doesn't exist for all element types, so some must be treated differently.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
May 1 '06 #16
On 01/05/2006 18:58, Evertjan. wrote:
Matt Kruse wrote on 01 mei 2006 in comp.lang.javascript:


[snip]
There is no need to set the defaultChecked value manually anyway - its
value is set by the browser. Why would you need to add an (invalid)
attribute into the html?


"defaultChecked" exists?


No. That's why Matt wrote, "an (invalid) attribute", where 'invalid' is
significant.

The checked and value attributes correspond to the defaultChecked and
defaultValue properties, respectively. Initially, these values are
copied to the checked and value properties, which represent the current
state of the form control.
I think Matt's follow-up covers the rest.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
May 1 '06 #17
Michael Winter wrote on 01 mei 2006 in comp.lang.javascript:
On 01/05/2006 18:58, Evertjan. wrote:
Matt Kruse wrote on 01 mei 2006 in comp.lang.javascript:


[snip]
There is no need to set the defaultChecked value manually anyway - its
value is set by the browser. Why would you need to add an (invalid)
attribute into the html?


"defaultChecked" exists?


No. That's why Matt wrote, "an (invalid) attribute", where 'invalid' is
significant.

The checked and value attributes correspond to the defaultChecked and
defaultValue properties, respectively. Initially, these values are
copied to the checked and value properties, which represent the current
state of the form control.
I think Matt's follow-up covers the rest.


I see here:
<http://msdn.microsoft.com/workshop/a...operties/defau
ltchecked.asp>
that it exists, also W3C approved, and told that I thought that Matt
implied it did not, what is cleared up now.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 1 '06 #18
Evertjan. wrote:
I see here:
<http://msdn.microsoft.com/workshop/a...operties/defau
ltchecked.asp>
that it exists, also W3C approved, and told that I thought that Matt
implied it did not, what is cleared up now.


It is a DOM element property.
It is not an HTML attribute.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
May 1 '06 #19
Thanks all for the replies.

May 18 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Wylie | last post by:
I am constructing an HTML questionnaire and one of the questions requires people to rate some choices from 1 to 5, where 1 is their favourite and 5 is their least favourite: Car Bus Taxi cab...
8
by: Neil | last post by:
I have a very puzzling situation with a database. It's an Access 2000 mdb with a SQL 7 back end, with forms bound using ODBC linked tables. At our remote location (accessed via a T1 line) the time...
15
by: Rey | last post by:
Howdy all. Appreciate your help with several problems I'm having: I'm trying to determine if the Visit subform (subformVisits) has a new record or been changed, i.e. dirty. The form that...
3
by: Casper Skovgaard | last post by:
I have a form with several fields (textbox, radio, checkbox), when the user submits the form I want to check if the user have changed anything. The check should be performed in the code-behind...
3
by: mike | last post by:
I'm writting a q&a chat application and hae a small refeshing IFRAME to check if there has been a new or modified chat item written, if so the main chat window refreshes and the datagrid gets...
2
by: magister | last post by:
Hello, I have a asp.net page with a form with 15 textboxes and a couple of dropdownlists....I want to know if their is a way (...serverside please...) of checking if any change has been made to...
3
by: rhobson2 | last post by:
Hello, I wrote a database applicaiton using Access XP (2002) and everything has been working good for the client until they purchased a couple of new computers with Access 2003. The meetings...
10
by: Cliff72 | last post by:
Is there a way in VBA to check if a linked table in the database has been updated? Example: I have a table "LedgerTemp" which is a direct link to a text file on the LAN "Ledger.txt" This text...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.