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

Home
JavaScript DHTML
1.Ajax Layer
2.Data Type
3.Date Time
4.Development
5.Document
6.Dojo toolkit
7.Event
8.Event onMethod
9.Ext JS
10.Form Control
11.GUI Components
12.HTML
13.Javascript Collections
14.Javascript Objects
15.Javascript Properties
16.jQuery
17.Language Basics
18.Mochkit
19.Mootools
20.Node Operation
21.Object Oriented
22.Page Components
23.Rico
24.Scriptaculous
25.Security
26.SmartClient
27.Style Layout
28.Table
29.Utilities
30.Window Browser
31.YUI Library
JavaScript DHTML » Form Control » Form Submit 




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].nameform.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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.