×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Philip Rehberger
Added: Sep 14, 2016 2:26 AM
Views: 16
Determines if a form is dirty by comparing the current value of each element with its default value
  1. /**
  2.  * Determines if a form is dirty by comparing the current value of each element
  3.  * with its default value.
  4.  *
  5.  * @param {Form} form the form to be checked.
  6.  * @return {Boolean} <code>true</code> if the form is dirty, <code>false</code>
  7.  *                   otherwise.
  8.  */
  9. function is_form_dirty(form) {
  10.     for (var i = 0; i &lt; form.elements.length; i++) {
  11.         var element = form.elements[i];
  12.         var type = element.type;
  13.         if (type == "checkbox" || type == "radio") {
  14.             if (element.checked != element.defaultChecked) {
  15.                 return true;
  16.             }
  17.         } else if (type == "hidden" || type == "password" || type == "text" || type == "textarea") {
  18.             if (element.value != element.defaultValue) {
  19.                 return true;
  20.             }
  21.         }else if (type == "select-one" || type == "select-multiple") {
  22.             for (var j = 0; j &lt; element.options.length; j++) {
  23.                 if (element.options[j].selected != element.options[j].defaultSelected) {
  24.                     return true;
  25.                 }
  26.             }
  27.         }
  28.     }
  29.     return false;
  30. }