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'
}
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'
}
Use serializeArray
var html = '<form><input type="text" value="v" name="nnn"></form>';
$(html).serializeArray();
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;
};