Skip to content

Instantly share code, notes, and snippets.

@carlohcs
Last active August 29, 2015 14:26
Show Gist options
  • Save carlohcs/7d28fc42a9aaf2b6e772 to your computer and use it in GitHub Desktop.
Save carlohcs/7d28fc42a9aaf2b6e772 to your computer and use it in GitHub Desktop.
/*!
* FormHelper
* @author Carlos Henrique Carvalho de Santana <[email protected]>
*
* Helpers to manipulate and get data from forms and elements
*/
var FormHelper = (function() {
return {
/**
* Return a array by {key: value} from a element
*
* @param object element
* @return array arr
*/
arrayKeyValue: function(element) {
var serial = $(element).serializeArray(),
arr = {},
name,
value;
//If not recognize a form, search for form's elements
if (serial.length == 0) {
serial = $(element)
.find('input,select,textarea')
.serializeArray();
}
$.each(serial, function(i, key) {
name = serial[i]['name'];
value = serial[i]['value'];
//If is a rray
if (name.indexOf('[') > -1) {
name = name.substr(0, name.length - 2);
//If the array not inicialized
if (!$.isArray(arr[name])) {
arr[name] = [];
}
//Add value to array
arr[name].push(value);
} else
arr[name] = value;
});
return arr;
}
}
})();
@carlohcs
Copy link
Author

carlohcs commented Aug 7, 2015

Helpers to manipulate and get data from forms and elements.

Methods:

  • arrayKeyValue: Return a array by {key: value} from a element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment