HTML and CSS Reference
In-Depth Information
});
return result;
}
});
})(jQuery);
If you now execute the following:
> o = $('form').toObject()
The variable o will contain an object, with properties reflecting the names of the field in
the form. It is therefore possible to execute the following:
> o.task
You will notice that the implementation starts by creating an empty object. Properties are
then added to the object for each form element. The implementation uses the [] bracket ap-
proach to add properties, rather than the “.” notation. This is important, because the name
of fields may not conform to the standards required by JavaScript when using the “.” nota-
tion.
Now that we have a toObject function, we can add a second function to de-serialize an ob-
ject back onto a form. In order to add a second function, we simply create a new property
in the object we are creating to extend jQuery. This function will be called fromObject and
accepts an object as a parameter:
(function($) {
$.fn.extend({
toObject: function() {
var result = {}
$.each(this.serializeArray(), function(i, v) {
result[v.name] = v.value;
});
return result;
},
fromObject: function(obj) {
}
});
})(jQuery);
Search WWH ::




Custom Search