Click here to Skip to main content
15,918,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 divs where 1st div has 2 text boxes while the other div has several text boxes
Manually I am adding the textbox name and value to an object for the 1st div. But for second div I am getting all the textbox names and values using jQuery to avoid having a large block of similar code.

var obj = {};
obj.LayoutName = document.getElementById("aSL_stripLayoutName").value;
obj.Version = document.getElementById("aSL_StripLayoutVersion").value;


Then I want to extend the object and add all textbox names and values to 'obj' using extend,

$("#aSL_stripLayoutDetails-UpperSection :text").each(function(){//TODO}); 


What I have tried:

I have tried the below and an example is working adding a known textbox name 'StripDimX' and value '333'
$.extend(obj, {StripDimX : "333"});


If I want to get the name I use this inside the below function
document.getElementById(this.id).name


and this for the value
document.getElementById(this.id).value


But my problem is how to add the names and values to the object?
$("#aSL_stripLayoutDetails-UpperSection :text").each(function(){
   //$.extend(obj, {StripDimX : "333"});
});

This of course does not work:
$("#aSL_stripLayoutDetails-UpperSection :text").each(function(){
 $.extend(obj, {document.getElementById(this.id).name:             document.getElementById(this.id).value});
});


Then I tried the below and name is ok, but the value is being added as an array:

$("#aSL_stripLayoutDetails-UpperSection :text").each(function(){
        var n = document.getElementById(this.id).name;
        var v = document.getElementById(this.id).value;
          $.extend(obj, { [n] : [v]});
});
Posted
Updated 10-Oct-17 20:52pm
v2

try

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        var obj = {};
        $(function () { 

        $("#aSL_stripLayoutDetails-UpperSection :text").each(function (index,elem) { 
            var o = {};
            o[elem.name] = elem.value;
            $.extend(obj, o);
        });
        alert( JSON.stringify( obj));


        });
    </script> 

</head>
<body>

    <div id="aSL_stripLayoutDetails-UpperSection">
        <input type="text" name="a" value="aa" />
        <input type="text" name="b" value="bb" />
        <input type="text" name="c" value="cc" />
        <input type="text" name="d" value="dd" />
    </div>

 


</body>
</html>
 
Share this answer
 
$("#aSL_stripLayoutDetails-UpperSection :text").each(function(){
        var n = document.getElementById(this.id).name;
        var v = document.getElementById(this.id).value;
        var tempObj = new createObject(n,v);
         $.extend(obj, tempObj);
});


function createObject(propName, propValue){
    this[propName] = propValue;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900