We help IT Professionals succeed at work.

onChange event to <FORM> element

bjdutta
bjdutta asked
on
2,002 Views
Last Modified: 2012-08-13
Hello,

I have more than hundreds form elements (text, dropdowns, checkboxes, etc.) in a form. I want to recognize if user changes any field value. I don't want to write onChange event on each form element. I wrote onChange on <FORM> and that works perfectly in Firefox. But IE is not recognizing the change event.

Anyone has any solution?

Thanks
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2004
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
similar implementation with Zyloch but I would call it on the window.onload event

<script>
window.onload = function(){
  var obj = document.forms[0];
  for(var i=0; i<obj.length; i++){
    obj.elements[i].onChange = function(){
      alert(this.name + 'has changed!');
    }
  }
}
</script>
Pravin AsarPrincipal Systems Engineer
CERTIFIED EXPERT
Top Expert 2005

Commented:
You can always use onkeyup , as

1. Whenever key is released it is called, (key-in data, paste by ctrl+C)

Pravin AsarPrincipal Systems Engineer
CERTIFIED EXPERT
Top Expert 2005

Commented:
Here is another approach, a bit lengthy, but works for all browsers.


Here is a complete code, which does not rely on anu user interaction event tracking.
Saves the original values , compares to the field values at the time of submission.

Alerts the what has been changed.

<html>
<head>
<title>Form Confirm Document</title>
</head>

<body>
<script language="javascript">
var formFlds=null;
function formFldData (fld) {
   this.name = fld.name;
   this.value = fld.value;
}
function SaveChanges(theForm) {
   formFlds = new Array();
   if (!theForm) { return false; }
   for (var ix=0; ix < theForm.elements.length; ix++) {
        formFlds[ix] = new formFldData(theForm.elements[ix]);
   }
}

function FindFld (fld) {
   var str = null;
   if (!fld) { return; }
   for (var ix=0; ix < formFlds.length; ix++) {
        if (!fld.name) { continue; }
        if (!formFlds[ix].name) { continue; }
       if (exactMatch(fld.name,formFlds[ix].name)) {
            if (!exactMatch(fld.value,formFlds[ix].value)) {
                str = '\n Field Name:' + fld.name + '\n    New Value:' + fld.value + '\n    Old Value:' + formFlds[ix].value;
                break;
            }
        }
   }
   return str;
}
function exactMatch(s1, s2) {
   if (!s1) { return false; }
   if (!s2) { return false; }
   if (s1.length == s2.length) {
      if (s1.match(s2) && s2.match(s1)) { return true; }
   }
   return false;
}
function CheckChanged(theForm) {
     var changed = false;
    if (!theForm) { return changed; }
    var msgStr = '';
    for (var ex=0; ex < theForm.elements.length; ex++) {
       var str = FindFld (theForm.elements[ex]);
       if (str) {
             changed = true;
               msgStr += str;
          }
    }
    if (changed) {
          msgStr = 'Following fields are changed\n' + msgStr;
    }
    else {
          msgStr = 'Nothing is changed\n';
    }
     alert (msgStr);
    return changed;    
}
function submitForm(theForm) {
if (CheckChanged(theForm)) {
     var conf = confirm ('Field Values changed, You want to continue');
     if (!conf) { return false; }
}
return true;
}
</script>
<form name="MyForm"  method="get" onsubmit="return submitForm(this);">
<br>First Name:<input type="text" name="firstname"  value="FIRSTNAME">
<br>Last Name:<input type="text" name="lastname" value="LASTNAME">
<br><input  type="reset" value="Reset"><input  type="submit" value="Submit Form">
</form>
<script language="javascript">
if (document.MyForm) { SaveChanges(document.MyForm); }
</script>
</body>
</html>

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.