Last active
August 29, 2015 14:26
-
-
Save carlohcs/7d28fc42a9aaf2b6e772 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* 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; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Helpers to manipulate and get data from forms and elements.
Methods: