For every HTML control, we know there is an ID, name, type and value attached to it. What is not known to all is that there is a 'defaultValue' as well for each control. So, using this 'default' behavior, we can check if there is a change in form-data.
/**
* Checks if the form data is changed
* @ param - none
* @returns boolean
*/
function isFormChanged() {
var formNo=document.forms.length;
var flag = true;
for (j=0;j
for(var i=0; i< max; i++) {
if (document.forms[j].elements[i].type =="textarea" || document.forms[j].elements[i].type =="text") {
if(document.forms[j].elements[i].value != document.forms[j].elements[i].defaultValue) {
return true;
}
}
if (document.forms[j].elements[i].type =="select-one") {
if(document.forms[j].elements[i].options[document.forms[j].elements[i].selectedIndex].selected != document.forms[j].elements[i].options[document.forms[j].elements[i].selectedIndex].defaultSelected) {
return true;
}
}
if (document.forms[j].elements[i].type =="select-multiple") {
if(document.forms[j].elements[i].options[document.forms[j].elements[i].selectedIndex].selected != document.forms[j].elements[i].options[document.forms[j].elements[i].selectedIndex].defaultSelected ) {
return true;
}
}
if (document.forms[j].elements[i].type =="checkbox" || document.forms[j].elements[i].type =="radio") {
if(document.forms[j].elements[i].checked != document.forms[j].elements[i].defaultChecked) {
return true;
}
}
}
}
return false;
}
So, as u can see the default behavior of textfield, textarea, radio button and checkbox has been used to arrive at this simple solution! You can even exclude certain controls from the check where the value has been assigned using javascript (i.e. once the page has already been rendered).
No comments:
Post a Comment