1

say, I have this:

var html = '<form><input type="text" value="v" name="nnn"></form>';

now I want to "unserialize" this, I mean to get an object/array with the key nnn and the value v. So,

{
    nnn: 'v',
    otherInputField: 'itsValue'
}
John Smith
  • 6,129
  • 12
  • 68
  • 123

1 Answers1

2

Use serializeArray

var html = '<form><input type="text" value="v" name="nnn"></form>';
$(html).serializeArray();

demo

If it absolutely must be a pure object, you can process the array into an object like

$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

Convert form data to JavaScript object with jQuery

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53