Lose the Tedium - Automate VGR Variable Initiation (Revisited)

One of the most popular posts from this blog has consistently been the Lose the Tedium - Automate VGR Variable Initiation post in which I showed how I've used a snippet of code to generate my "INIT" section for my VGR. This has been always been helpful to me when initially building forms, especially long ones (it gives a certain peace of mind even on short ones).

The only "problem" with that code is that it relies on jQuery for much of its functionality. I've gotten the request more than once to rewrite it using "native" JavaScript (ie. no additional required libraries).

/*
* vgr_init_js() - Automation to build VGR initialization code
* http://heo-iforms.blogger.com/
*
* Version: 1.0
* Copyright 2014 Scott Morris - http://heo-iforms.blogger.com/
*
* Dual licensed under MIT or GPLv2 licenses
*   http://en.wikipedia.org/wiki/MIT_License
*   http://en.wikipedia.org/wiki/GNU_General_Public_License
*
*/
function vgr_init_js() {
  var txt = {}, chk = {}, rad = {}, cbo = {}, btn = {}, output = '', obj;

  var sels = document.getElementsByTagName('SELECT');
  var inputs = document.getElementsByTagName('INPUT');

  for (var i=0; i < inputs.length; i++) {
    switch (inputs[i].type) {
      case 'text':
        txt[inputs[i].name] = inputs[i].value;
        break;

      case 'checkbox':
        chk[inputs[i].name] = (inputs[i].checked) ? inputs[i].value : '';
        break;

      case 'submit':
        btn[inputs[i].name] = inputs[i].value;
        break;

      case 'radio':
        if (rad.hasOwnProperty(inputs[i].name)) {
          rad[inputs[i].name] = (inputs[i].checked) ? inputs[i].value : rad[inputs[i].name];
        } else {
          rad[inputs[i].name] = (inputs[i].checked) ? inputs[i].value : '';
        }
        break;
    }
  }

  for (var i=0; i < sels.length; i++) {
    cbo[sels[i].name] = sels[i].value;
  }

  // Checkboxes
  output += '# Checkboxes ' + Array(86).join('-') + '\n';
  for (obj in chk) {
    output += 'INIT,SET,CHECKBOX,' + obj + ',TO,"' + chk[obj] + '"\n';
  }

  // Text Boxes
  output += '\n# Text Boxes ' + Array(86).join('-') + '\n';
  for (obj in txt) {
    output += 'INIT,SET,TEXT,' + obj + ',TO,"' + txt[obj] + '"\n';
  }

  // Drop Down Boxes
  output += '\n# Drop Down Boxes ' + Array(81).join('-') + '\n';
  for (obj in cbo) {
    output += 'INIT,SET,SELECT,' + obj + ',TO,"' + cbo[obj] + '"\n';
  }

  // Radio Buttons
  output += '\n# Radio Buttons ' + Array(83).join('-') + '\n';
  for (obj in rad) {
    output += 'INIT,SET,RADIO,' + obj + ',TO,"' + rad[obj] + '"\n';
  }

  // Buttons
  output += '\n# Buttons ' + Array(89).join('-') + '\n';
  for (obj in btn) {
    output += 'INIT,SET,HIDDEN,' + obj + ',TO,"' + btn[obj] + '"\n';
  }

  output = '<' + 'pre>' + output + '<' + '/pre>';


  myWindow=window.open('','','width=650,height=600,location=no,menubar=no,status=no,left=10,top=10');
  myWindow.title = "VGR Init Code";
  myWindow.document.write(output);
  myWindow.focus();
}

You can trigger this code in any number of ways. As I mentioned in an earlier post, you can "tie" it to clicking a picture while holding Shift+Ctrl. If you'd like more information on how to do that or to throw other ideas around, feel free to reach out to me.

Scott Morris is available for training, mentoring, troubleshooting, and iForms consulting. Find out more at www.thinkiforms.com