secret
Last active

  • Download Gist
form-values.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
YUI.add("form-values", function(Y){
var _form,
_values
;
Form = function(selector) {
if(selector != null) {
var node = Y.one(selector);
if(node && node.get('nodeName').toLowerCase() === 'form') {
_form = node;
return _form;
}
}
_form = null;
return _form;
};
Form.setForm = function(selector) {
return Form(selector);
};
Form.getForm = function(){
return _form;
};
Form.getFormValues = function(){
return Form._setFormValues();
};
Form._setFormValues = function(){
_values = {};
if(Form.getForm() !== null) {
_form.get('elements').each(function(field){
var type = field.get('nodeName') + ':' + (field.get('type') || ''),
name = field.get('name'),
value;
switch (type.toLowerCase()) {
case 'input:text' : // fall through intentional
case 'input:hidden' :
case 'textarea:' :
case 'select:' :
value = field.get('value');
break;
case 'input:radio' : // fall through intentional
case 'input:checkbox' :
value = field.get('checked') ? field.get('value') : undefined;
break;
}
if(value !== undefined) {
if (name in _values) {
if(!Y.Lang.isArray(_values[name])) {
_values[name] = [_values[name]];
}
_values[name].push(value);
}else{
_values[name] = value;
}
}
});
}
return _values;
};
}, '0.1', {requires : ['node']});
 
if(Y.Form !== undefined || Y.Form !== null) {
Y.mix(Y.Form, Form);
}else{
Y.Form = Form;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.