Skip to content

Instantly share code, notes, and snippets.

@thinkt4nk
Created September 13, 2011 15:48
Show Gist options
  • Save thinkt4nk/1214168 to your computer and use it in GitHub Desktop.
Save thinkt4nk/1214168 to your computer and use it in GitHub Desktop.
jQuery Utils
(function($) {
var serialize = function(type,form) {
var data = {};
var first_run_ignore_types = ['submit','checkbox'];
form.find('input, select').each(function() {
var input_type = $(this).attr('type');
var input_name = $(this).attr('name');
if (first_run_ignore_types.indexOf(input_type) === -1) {
// process and push to data
var input_value = $(this).val();
data[input_name] = input_value;
}
else if (input_type === 'checkbox') {
// process checkbox and push to data
var input_value = $(this).attr('checked') ? 1 : 0;
data[input_name] = input_value;
}
});
if (type === 'array')
{
return dataToArray(data);
}
};
var dataToArray = function(data)
{
return data;
};
$.fn.serializeArray = function(options)
{
if (this.length > 0)
return serialize('array',$(this[0]))
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment