HTML and CSS Reference
In-Depth Information
//
jQuery has a number of utility functions available with “$.” prefixes. Unlike oth-
er jQuery functions, these are not used for selecting elements, and they cannot
be invoked on a set of elements.
This particular function will iterate over an array and pass each index and value to the call-
back function we provide. To see this in action, try executing the following:
> $.each([1,4,8,16,32], function(i, v) {
console.log('index: '+i);
console.log('value: '+v)
});
This should print out:
index: 0
value: 1
index: 1
value: 4
index: 2
value: 8
index: 3
value: 16
index: 4
value: 32
Unlike the standard JavaScript for and while loops, the $.each helper has the ability to it-
erate over both arrays and objects.
Now that the skeleton of toObject is in place, we can complete the implementation as fol-
lows:
(function($) {
$.fn.extend({
toObject: function() {
var result = {}
$.each(this.serializeArray(), function(i, v) {
result[v.name] = v.value;
 
Search WWH ::




Custom Search