Convert a passed form reference to a string formatted like a JavaScript array of objects : Form Submit « Form Control « JavaScript DHTML






Convert a passed form reference to a string formatted like a JavaScript array of objects

   

// Read the name, id, type, and value of one form control element
// as requested by form2ArrayString()
function formObj2String(obj) {
  var output = "{";
  if (obj.name) {
    output += "name:'" + obj.name + "',";
  }
  if (obj.id) {
    output += "id:'" + obj.id + "',";
  }
  output += "type:'" + obj.type + "',";
  switch (obj.type) {
    case "radio":
      if (obj.name) {
        obj = document.forms[0].elements[obj.name];
        var radioVal = "value:false,index:-1";
        for (var i = 0; i < obj.length; i++) {
          if (obj[i].checked) {
            radioVal = "value:true,index:" + i;
            i = obj.length;
          } 
        }
        output += radioVal;
      } else {
        output += "value:" + obj.checked;
      }
      break;
    case "checkbox":
      output += "value:" + obj.checked;
      break;
    case "select-one":
      output += "value:" + obj.selectedIndex;
      break;
    case "select-multiple":
      output += "value:" + obj.selectedIndex;
      break;
    case "text":
      output += "value:'" + escape(obj.value) + "'";
      break;
    case "textarea":
      output += "value:'" + escape(obj.value) + "'";
      break;
    case "password":
      output += "value:'" + escape(obj.value) + "'";
      break;
    case "hidden":
      output += "value:'" + escape(obj.value) + "'";
      break;
    default:
      output += "";
  }
  output += "}"
  return output;
}

// Convert a passed form reference to a string formatted like
// a JavaScript array of objects
function form2ArrayString(form) {
  var elem, lastName = "";
  var output = "[";
  for (var i = 0; i < form.elements.length; i++) {
    elem = form.elements[i];
    if (elem.name && (elem.name != lastName)) {
      output += formObj2String(form.elements[i]) + ",";
      lastName = elem.name;
    }
  }
  output = output.substring(0, output.length-1) + "]";
  return output;
}

// Distribute form control values from another source to the
// controls in this page's form, whose names/ids match those
// of the original form controls
function string2FormObj(form, str) {
  var elem, objArray = eval(str);
  for (var i = 0; i < objArray.length; i++) {
    elem = (objArray[i].name) ? form.elements[objArray[i].name] : document.getElementById(objArray[i].id);
    switch (objArray[i].type) {
      case "radio":
        if (objArray[i].name && objArray[i].value && objArray[i].index >= 0) {
          elem = elem[objArray[i].index];
        }
        elem.checked = objArray[i].value;
        break;
      case "checkbox":
        elem.checked = objArray[i].value;
        break;
      case "select-one":
        elem.selectedIndex = objArray[i].value;
        break;
      case "select-multiple":
        elem.selectedIndex = objArray[i].value;
        break;
      default:
        elem.value = unescape(objArray[i].value);
    }
  }
}


           
         
    
    
  








Related examples in the same category

1.Form Submit action
2.Return the method used when sending form data
3.Submit a form Via Enter
4.Use form select control to trigger the action
5.form on submit event
6.Last-Minute Checking Before Form Submission
7.Use Image to trigger submit and reset actions
8.Submit a form by sending out an email
9.Adjusting a Server Submission Action
10.Demo: Ajax form submit
11.html input mask