Skip to content

Instantly share code, notes, and snippets.

@frentsel
Last active July 27, 2018 12:04
Show Gist options
  • Save frentsel/7d82a8da0c662700f94d3af11972f01d to your computer and use it in GitHub Desktop.
Save frentsel/7d82a8da0c662700f94d3af11972f01d to your computer and use it in GitHub Desktop.
Tiny jQuery plugin fData for works with form as a json object
$.fn.fData = function(_data) {
var $form = this;
if (!_data) {
return $form.serializeArray().reduce(function(obj, field) {
obj[field.name] = field.value;
return obj;
}, {});
}
$.each(_data, function(key, val) {
$form[0][key].value = val;
});
return _data;
};
// Example
var form = $('form');
// Return json object of form
console.log(form.fData());
// Fill form
form.fData({
userName: 'Sanya',
email: '[email protected]',
age: 32
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment