utils/form.js
/**
 * Pack the values of all contained children's form input elments into an object.
 * Function will scan the children for any valid element (meaning, id needs a name).
 * @param form
 */
Pack the values of all contained children's form input elments into an object.
Function will scan the children for any valid element (meaning, id needs a name).
@param form
Pack the values of all contained children ' s form input elments into an object . Function will scan the children for any valid element ( meaning id needs a name ) . @param form
function formData(form) {
    var formTarget = null;
    if (form instanceof Element) {
        formTarget = form;
    } else {
        formTarget = document.querySelector(form);
    }
    var elements = formTarget.getElementsByTagName('input')
    var data = {};
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        if (!element.hasAttribute('name')) {
            continue;
        }
        if (element.type === 'text'
            || element.type === 'hidden'
            || element.type === 'password'
            || (element.type === 'checkbox' && element.value)) {
            data[element.getAttribute('name')] = element.value;
        } else if (element.type === 'checkbox') {
            data[element.getAttribute('name')] = !!element.checked;
        } else if (element.type === 'radio' && element.checked) {
            data[element.getAttribute('name')] = !!element.checked;
        }
    }
    var textareas = formTarget.getElementsByTagName('textarea');
    for (var i = 0; i < textareas.length; i++) {
        var textarea = textareas[i];
        if (!textarea.hasAttribute('name')) {
            continue;
        }
        data[textarea.getAttribute('name')] = textarea.value;
    }
    var selects = form.getElementsByTagName('select');
    for (var i = 0; i < selects.length; i++) {
        var select = selects[i];
        if (!select.hasAttribute('name') || select.selectedIndex === -1) {
            continue;
        }
        if (select.hasAttribute('multiple')) {
            var selectValues = [];
            for (var j = 0; j < select.options.length; j++) {
                var option = select.options[j];
                if (option.selected) {
                    selectValues.push(option.value || option.text);
                }
            }
            data[select.getAttribute('name')] = selectValues;
        } else {
            var option = select.options[select.selectedIndex];
            data[select.getAttribute('name')] = option.value || option.text;
        }
    }
    return data;
}
function formData ( form ) { var formTarget = null ; if ( form instanceof Element ) { formTarget = form ; } else { formTarget = document . querySelector ( form ) ; } var elements = formTarget . getElementsByTagName ( 'input' ) var data = { } ; for ( var i = 0 ; i < elements . length ; i ++ ) { var element = elements [ i ] ; if ( ! element . hasAttribute ( 'name' ) ) { continue ; } if ( element . type === 'text' || element . type === 'hidden' || element . type === 'password' || ( element . type === 'checkbox' && element . value ) ) { data [ element . getAttribute ( 'name' ) ] = element . value ; } else if ( element . type === 'checkbox' ) { data [ element . getAttribute ( 'name' ) ] = ! ! element . checked ; } else if ( element . type === 'radio' && element . checked ) { data [ element . getAttribute ( 'name' ) ] = ! ! element . checked ; } } var textareas = formTarget . getElementsByTagName ( 'textarea' ) ; for ( var i = 0 ; i < textareas . length ; i ++ ) { var textarea = textareas [ i ] ; if ( ! textarea . hasAttribute ( 'name' ) ) { continue ; } data [ textarea . getAttribute ( 'name' ) ] = textarea . value ; } var selects = form . getElementsByTagName ( 'select' ) ; for ( var i = 0 ; i < selects . length ; i ++ ) { var select = selects [ i ] ; if ( ! select . hasAttribute ( 'name' ) || select . selectedIndex === - 1 ) { continue ; } if ( select . hasAttribute ( 'multiple' ) ) { var selectValues = [ ] ; for ( var j = 0 ; j < select . options . length ; j ++ ) { var option = select . options [ j ] ; if ( option . selected ) { selectValues . push ( option . value || option . text ) ; } } data [ select . getAttribute ( 'name' ) ] = selectValues ; } else { var option = select . options [ select . selectedIndex ] ; data [ select . getAttribute ( 'name' ) ] = option . value || option . text ; } } return data ; }
Pack the values of all contained children's form input elments into an object.
Pack the values of all contained children ' s form input elments into an object .
//var textareas = Array.prototype.slice.call(formTarget.getElementsByTagName('textarea')); //elements.push.apply(elements, textareas); // serialize as string // selects are either flattened or packed as arrays (for multiple) //console.log(data);