Lose the Tedium - Automate VGR Variable Initiation

Have you ever put together a long iForm with what feels like hundreds of checkboxes, radio buttons, dropdowns, text boxes, and buttons only to realize:
"Oh wait, now I have to map this monster."

I know I've been there, so I decided to do something about it.
As long as you use jQuery in your iForms, you can use this function to really give you a quick boost in your VGR code:
/*
* vgr_init() - Automation to build VGR initialization code
* http://heo-iforms.blogger.com/
*
* Version: 1.0
* Copyright 2012 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
*
* Requires: jQuery
*
*/
function vgr_init() {
    var $dropDownBoxes = $('select'),
        $checkBoxes = $('input:checkbox'),
        $radioButtons = $.unique($('input:radio').map(function() { return $(this).attr('name'); }).get()),
        $textBoxes = $('input[type="text"]'),
        $buttons = $('input:button, input:reset, input:submit'),
        output = '', $item, itemID, i;
    
    // Drop Down Boxes
    output += '# Drop Down Boxes ' + Array(81).join('-') + '\n';
    for (i=0;i<$dropDownBoxes.length;i++) {
        $item = $($dropDownBoxes[i]);
        itemID = ($item.attr('id') !== undefined) ? $item.attr('id') : $item.attr('name');
        output += 'INIT,SET,SELECT,' + itemID + ',TO,';
        if ($item.val() !== '')
            output += '"' + $item.val() + '"';
        output += '\n';
    }
    
    // Checkboxes
    output += '\n# Checkboxes ' + Array(86).join('-') + '\n';
    for (i=0;i<$checkBoxes.length;i++) {
        $item = $($checkBoxes[i]);
        itemID = ($item.attr('id') !== undefined) ? $item.attr('id') : $item.attr('name');
        output += 'INIT,SET,CHECKBOX,' + itemID + ',TO,';
        if ($item.attr('checked') !== undefined)
            output += '"' + $item.val() + '"';
        output += '\n';
    }
    
    // Radio Buttons
    output += '\n# Radio Buttons ' + Array(83).join('-') + '\n';
    for (i=0;i<$radioButtons.length;i++) {
        output += 'INIT,SET,RADIO,' + $radioButtons[i] + ',TO,';
        if ($('input:radio[name="' + $radioButtons[i] + '"]:checked').val() !== undefined) {
            output += '"' + $('input:radio[name="' + $radioButtons[i] + '"]:checked').val() + '"';
        }
        output += '\n';
    }
    
    // Text Boxes
    output += '\n# Text Boxes ' + Array(86).join('-') + '\n';
    for (i=0;i<$textBoxes.length;i++) {
        $item = $($textBoxes[i]);
        itemID = ($item.attr('id') !== undefined) ? $item.attr('id') : $item.attr('name');
        output += 'INIT,SET,TEXT,' + itemID + ',TO,';
        if ($item.val() !== '')
            output += '"' + $item.val() + '"';
        output += '\n';
    }
    
    // Buttons
    output += '\n# Buttons ' + Array(89).join('-') + '\n';
    for (i=0;i<$buttons.length;i++) {
        $item = $($buttons[i]);
        itemID = ($item.attr('id') !== undefined) ? $item.attr('id') : $item.attr('name');
        output += 'INIT,SET,HIDDEN,' + $item.attr('id') + ',TO,\n';
    }
    
    output = '
' + output + '
'; 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(); }


I usually bind this function to a "hidden" element. For instance, if you hold Ctrl + Shift and click an image at the bottom of the form, it will run this code and show the results in a new window. To do this, you can put this in your header code:
$('#footerImage').on('click',function(e) { if (e.shiftKey && e.ctrlKey) { vgr_init(); }});

See For Yourself


  

Update

I've revisited this post and re-created the functionality using "native" JavaScript (not relying on jQuery or any other library). If you'd like that code, see the updated post.

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

2 comments:

  1. I love it!! I was using a js to get form elements and then excel to concatonate. This is way better!

    ReplyDelete
  2. I've done a lot of automation stuff with my iForms, and this is simply one of the most awesome ideas I've seen.

    ReplyDelete