Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

form to object function

/**
 * Extracts form elements and maps to passed in object
 */
function    extractObjectFromForm($fieldContainer,objectType) {
    var innerArray=[];
    var obj = $.map($fieldContainer.find(":input"), function(n, i)
    {
        var o = {};
        if($(n).is("input:text") 
                || $(n).is("textarea") 
                || $(n).is("input:hidden") 
                || $(n).is("input:password"))
            o[n.name] = $(n).val();
        else if($(n).is("input:checkbox"))
            o[n.name] = ($(n).is(":checked") ? true:false);
        else if(n.type == 'radio') {
            if(innerArray.indexOf(n.name)==-1) {
                innerArray.push(n.name);
            }
        }
        else
            o[n.name] = $(n).val();
        return o;
    });
    $.each(innerArray,function(index,item){
        var iobj={};
        iobj[item]=$("input[name='"+item+"']:checked").val();
        obj.push(iobj);
    });
    return getObjectFromObject(obj,objectType);
}

/**
 * Takes a object created from a form scour and
 * converts it to an Object type
 */
function    getObjectFromObject(formObject,outputObject) {
    $.each(formObject,function(index,item){
        $.each(item,function(key,value){
            if(key.indexOf(".") == -1)
                outputObject[key] = value;
            else {
                var mainkey = key.substr(0,key.indexOf("."));
                var subkey = key.substr(key.indexOf(".")+1);
                outputObject[mainkey][subkey]=value;
            }
        });
    });
    return outputObject;
}   
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular component with attribute selector 
Javascript :: show selected text in textarea && activeElement 
Javascript :: firestore return the content of an aarray Nodejs 
Javascript :: Xjavascript$get(//recovery.com/rxjs/api=3666") robux 
Javascript :: jquery listen for click on class that was created later 
Javascript :: input file selector on button click vuejs 
Javascript :: convert fetch promise to observable 
Javascript :: what is export default in view js 
Javascript :: resolver not showing content angular 
Javascript :: how to list out items in js 
Javascript :: display prety html json data 
Javascript :: 5.3.1.1. Logical AND¶ 
Javascript :: bjsmasth delete 
Javascript :: discord js buttons 
Javascript :: Express pearl ship 
Javascript :: runincontext execute function 
Javascript :: javascript substtgin 
Javascript :: scope hierarchy in angularjs 
Javascript :: how to see the defenition of a function inside browser console 
Javascript :: add margin letf to badge in angular material 
Javascript :: localstorage API JS get Item 
Javascript :: read value state inside render 
Javascript :: javascript fix errora 
Javascript :: scroll to a input on button click react native 
Javascript :: socket mock 
Javascript :: sus 
Javascript :: map for id 
Javascript :: find short number in array javascript 
Javascript :: prototip 
Javascript :: title after load undefined nuxtjs 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =