3

Trying to create dynamic HTML forms and save them, I can create dynamic forms using bootstrap but on submit i am struggling to create JSON of this dynamic form. I am looking to save something like this

{
 "form" :
    [

        {
            "name" : "username",
            "id" : "txt-username",
            "caption" : "Username",
            "type" : "text",
            "placeholder" : "E.g. [email protected]"
        },
        {
            "name" : "password",
            "caption" : "Password",
            "type" : "password"
        },
        {
            "type" : "submit",
            "value" : "Login"
        }
    ]
}

I am not sure how i can achieve this.

4
  • what exacltly you want to achieve? you want to send all your form propertis? or only inputs? Commented Dec 19, 2014 at 10:19
  • All form properties which i created dynamically (html form). I want to save that JSON object of that form properties (type, value(if any), name, id..). Commented Dec 19, 2014 at 10:21
  • for send only values you can use $("form").serializeArray();...but for attributes I dont know...I think attr() function may help you. Commented Dec 19, 2014 at 10:25
  • I was trying with DOM, but there should be some standard way.. Ahhh Commented Dec 19, 2014 at 10:28

1 Answer 1

5

This should do it:

function getAttrs(DOMelement) {
    var obj = {};
    $.each(DOMelement.attributes, function () {
        if (this.specified) {
            obj[this.name] = this.value;
        }
    });
    return obj;
}

$("form").each(function () {
    var json = {
        "form": []
    };
    $(this).find("input").each(function () {
        json.form.push(getAttrs(this));
    });

    $(this).find("select").each(function () {
        var select = getAttrs(this);
        select["type"] = "select";
        var options = [];
        $(this).children().each(function () {
            options.push(getAttrs(this));
        });
        select["options"] = options;
        json.form.push(select);
    });

    console.log(json);
});

DEMO: http://jsfiddle.net/j1g5jog0/

Update: http://jsfiddle.net/j1g5jog0/5/

Sign up to request clarification or add additional context in comments.

4 Comments

There is one issue, <select> and its <options> are not captured by this solution.
@user986508 Ok, how you do you want select stored then, something like this? {"type": select, "options": [{...}, {...}, {...}]}
yes something like this, { "form": [ { "name": "username", "id": "txt-username", "caption": "Username", "type": "text", "placeholder": "E.g. [email protected]" }, { "name": "password", "caption": "Password", "type": "password" }, { "name": "country", "type": "select", "options": [ { "name": "Japan" }, {"name": "Thailand"},{"name": "Spain"}]}]}
@caeth Thank you for the examples you have provided.Can you please help me that how can we see the data in successfully created in json object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.