Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 981647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:34:33+00:00 2026-05-16T04:34:33+00:00

Is there a way to turn a form into a complex JavaScript object based

  • 0

Is there a way to turn a form into a complex JavaScript object based some structured form?

Now, I have no idea if this should be done in a better way, but basically I want something like this:

<form>
   <input name="Foo" value="1" />
   <input name="Parent.Child1" value="1" />
   <input name="Parent.Child2" value="2" />
</form>

and I want this in JavaScript:

var form = GetForm();
var obj = ConvertFormToComplexObject(form);

//
// Followings should be true
// 
// obj.Foo == 1;
// obj.Parent != null
// obj.Parent.Child1 == 1;
// obj.Parent.Child2 == 2;
// 

Any suggestions?

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:34:34+00:00Added an answer on May 16, 2026 at 4:34 am

    I wrote a plugin to do just that.
    Hope it will help someone out there.
    Let me know if you find any bugs.

    Here’s the code for serializeObject.js:

    $.fn.serializeObject = function() {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
    
            var arrayIndex = function(name) {
                //note: 2d array not handled
    
                var startIndex = name.indexOf('[');
                var endIndex = name.indexOf(']');
    
                if (startIndex == -1 || endIndex == -1 || endIndex != name.length - 1)
                    return null;
    
                return name.substr(startIndex + 1, endIndex - startIndex - 1);
            }
    
            var trimArrayIndex = function(name) {
                var startIndex = name.indexOf('[');
                return name.substr(0, startIndex);
            }
    
            var createObject = function(obj, className, value) {
                if (className.length == 0)
                    return;
    
                var classNames = className.split(".");
    
                if (classNames.length == 1) {
    
                    if (obj[classNames[0]] == null) {
                        obj[classNames[0]] = value;
                    }
                    else if (obj[classNames[0]] instanceof Array) {
                        obj[classNames[0]].push(value);
                    }
                    else {
                        var temp = obj[classNames[0]];
    
                        obj[classNames[0]] = new Array();
                        obj[classNames[0]].push(temp);
                        obj[classNames[0]].push(value);
                    }
    
                    return;
                }
    
                var index = arrayIndex(classNames[0]);
                var isArray = index != null;
    
                if (!isArray) {
                    if (obj[classNames[0]] == null) {
                        obj[classNames[0]] = new Object();
                    }
    
                    createObject(obj[classNames[0]], className.substr(classNames[0].length + 1), value);
                }
                else {
                    var aryName = trimArrayIndex(classNames[0]);
    
                    if (obj[aryName] == null) {
                        obj[aryName] = new Array();
                    }
                    else if (!obj[aryName] instanceof Array) {
                        throw "unable to serialize " + aryName + " as an array";
                    }
    
                    var ary = obj[aryName];
                    var nextObj;
    
                    if (ary[parseInt(index)] == null) {
                        ary[parseInt(index)] = new Object();
                    }
    
                    nextObj = ary[parseInt(index)];
    
                    createObject(nextObj, className.substr(classNames[0].length + 1), value);
                }
            }
    
            createObject(o, this.name, this.value || '');
        });
        return o;
    };
    
    $.fn.replaceStarWithIndex = function() {
        var a = this.serializeArray();
        var form = this;
    
    
        var arrayIndex = function(name) {
            var startIndex = name.indexOf('[');
            var endIndex = name.indexOf(']');
    
            if (startIndex == -1 || endIndex == -1) {
                return null;
            }
    
            return name.substr(startIndex + 1, endIndex - startIndex - 1);
        }
    
        var trimArrayIndex = function(name) {
            var startIndex = name.indexOf('[');
            return name.substr(0, startIndex);
        }
    
        for (var key in a) {
            var index = arrayIndex(a[key].name);
    
            if (index == null || index != "*") {
                continue;
            }
    
            var count = 0;
            var trimName = trimArrayIndex(a[key].name);
    
            while (true) {
                var elementName = a[key].name.replace('*', count);
                var element = form[0][elementName];
    
                if (element == null) {
                    $(form[0][a[key].name]).first().attr('name', elementName);
                    break;
                }
                count++;
            }
        }
    }   
    

    Here’s the test:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Test</title>
        <link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css" media="screen" />
    
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
        <script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js" ></script>
        <script type="text/javascript" src="serializeObject.js" ></script>
    </head>
    
    <body>
    
    <h1 id="qunit-header">Test serializeObject</h1>
    <h2 id="qunit-banner"></h2>
    <h2 id="qunit-userAgent"></h2>
    <ol id="qunit-tests"></ol>
    
    <!--Test Form -->
    
    <form id="form1" style="display:none;">
        <input type="text" name="Parent.Child1" value="child1"/>
    
        <input type="text" name="Parent.Child2" value="child2a"/>
        <input type="text" name="Parent.Child2" value="child2b"/>
    
        <input type="text" name="Parent.Child3" value="3"/>
        <input type="text" name="Parent.Child3" value="2"/>
        <input type="text" name="Parent.Child3" value="1"/>
    
        <input type="text" name="Parent.Child4[0].Child1" value="11" />
        <input type="text" name="Parent.Child4[0].Child2" value="aa" />
    
        <input type="text" name="Parent.Child4[1].Child1" value="22" />
        <input type="text" name="Parent.Child4[1].Child2" value="bb" />
    
    
    
    </form>   
    
    <form id="form2" style="display:none;">
        <input type="text" name="Child1[0].Child1" value="0" />
        <input type="text" name="Child1[*].Child1" value="1" />
        <input type="text" name="Child1[*].Child1" value="2" />
    
        <input type="text" name="Child2[2]" value="2" />
        <input type="text" name="Child2[*]" value="0" />
        <input type="text" name="Child2[*]" value="1" />
    </form> 
    
    <script type="text/javascript">
        $(document).ready(function() {
            var obj = $('#form1').serializeObject();
    
            test("Parent should exist", function() {
                equals(true, obj.Parent != null);
            });
    
            test("Child1 should exist within parent", function() {
                equals(true, obj.Parent.Child1 != null);
            });
    
            test("Should create array for items with same name", function() {
                equals("child2a", obj.Parent.Child2[0]);
                equals("child2b", obj.Parent.Child2[1]);
                equals("3", obj.Parent.Child3[0]);
                equals("2", obj.Parent.Child3[1]);
                equals("1", obj.Parent.Child3[2]);
            });
    
    
            test("Should allow array of objects", function() {
                equals("11", obj.Parent.Child4[0].Child1);
                equals("aa", obj.Parent.Child4[0].Child2);
                equals("22", obj.Parent.Child4[1].Child1);
                equals("bb", obj.Parent.Child4[1].Child2);
            });
    
            $('#form2').replaceStarWithIndex();
    
            test("Should replace * with index", function() {
                equals("0", $('#form2 input[name="Child1[0].Child1"]').val());
                equals("1", $('#form2 input[name="Child1[1].Child1"]').val());
                equals("2", $('#form2 input[name="Child1[2].Child1"]').val());
    
                equals("0", $('#form2 input[name="Child2[0]"]').val());
                equals("1", $('#form2 input[name="Child2[1]"]').val());
                equals("2", $('#form2 input[name="Child2[2]"]').val());
            });
    
        });
    </script>
    
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to turn off this mode? I must have clicked it
Is there a way to turn a char into a String or a String
Is there a way in python to turn a try/except into a single line?
Is there a way to turn off all visual updates to a .NET form
I have a C# form whose input I am aiming to turn into an
Is there a simple way to turn off form submit behaviour of the page
Is there a way I can turn this: <%= f.datetime_select :truckleft, :start_year => Date.current.year,
The situation is this: I have some JavaScript that creates an interactive dialogue (and
Is there a way to turn ON UAC programmatically with C#? I know, everyone
Is there a way to turn off intellisense in Visual Studio 2008? I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.