Skip to content

Instantly share code, notes, and snippets.

@aportnov
Created November 17, 2010 04:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aportnov/702981 to your computer and use it in GitHub Desktop.
Simple jQuery plugin to collect data from the inputs
(function( $ ){
$.fn.collectFields = function(fieldSelector) {
var convert = function(type, value) {
switch (type) {
case "int" : return value ? parseInt(value) : 0;
case "float" : return value ? parseFloat(value): 0.0;
case "boolean" : return Boolean(value);
default : return value;
}
};
var result = [];
this.each(function() {
var fields = {};
$(this).find(fieldSelector).each(function(){
var name = $(this).attr('name');
if (name) {
fields[name] = convert($(this).attr("json-type"), $(this).val());
}
});
result.push(fields);
});
return result;
};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment