Last active
January 31, 2021 13:59
-
-
Save Sandip124/832182eaa8fc3838340a5e41e0d3f0fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks if changes are made to a form | |
* credits to Craig Buckler "http://www.sitepoint.com/javascript-form-change-checker/" | |
* @param {type} form | |
* @returns {Array|formChanges.changed} | |
*/ | |
function IsFormChanged(form) { | |
if (typeof(form) === "string"){ | |
form = document.getElementById(form); | |
} | |
if (!form || !form.nodeName || form.nodeName.toLowerCase() !== "form"){ | |
return null; | |
} | |
var changed = [], n, c, def, o, ol, opt; | |
for (var e = 0, el = form.elements.length; e < el; e++) { | |
n = form.elements[e]; | |
c = false; | |
switch (n.nodeName.toLowerCase()) { | |
// select boxes | |
case "select": | |
def = 0; | |
for (o = 0, ol = n.options.length; o < ol; o++) { | |
opt = n.options[o]; | |
c = c || (opt.selected !== opt.defaultSelected); | |
if (opt.defaultSelected){ | |
def = o; | |
} | |
} | |
if (c && !n.multiple){ | |
c = (def !== n.selectedIndex); | |
} | |
break; | |
//input/textarea | |
case "textarea": | |
case "input": | |
switch (n.type.toLowerCase()) { | |
case "checkbox": | |
case "radio": | |
// checkbox / radio | |
c = (n.checked !== n.defaultChecked); | |
break; | |
default: | |
// standard values | |
c = (n.value !== n.defaultValue); | |
break; | |
} | |
break; | |
} | |
if (c){ | |
changed.push(n); | |
} | |
} | |
//return true or false based on the length of variable "changed" | |
if(changed.length > 0){ | |
return true; | |
} | |
else{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment