Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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