Last active
July 27, 2018 12:04
-
-
Save frentsel/7d82a8da0c662700f94d3af11972f01d to your computer and use it in GitHub Desktop.
Tiny jQuery plugin fData for works with form as a json object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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