Skip to content

Instantly share code, notes, and snippets.

@KB1RMA
Last active August 29, 2015 13:57
Show Gist options
  • Save KB1RMA/0542858aeeacfc6b65bc to your computer and use it in GitHub Desktop.
Save KB1RMA/0542858aeeacfc6b65bc to your computer and use it in GitHub Desktop.
A handy function to take an object and fill an HTML form with those values. It would be nice to not use $.each() as it's slow, but this clean and easy.
function fillForm(formSelector, data) {
$.each(data, function (name, val) {
var
$el = $(formSelector + ' [name="' + name + '"]'),
type = $el.attr('type');
switch (type) {
case 'checkbox':
$el.attr('checked', 'checked');
break;
case 'radio':
$el.filter('[value="' + val + '"]').attr('checked', 'checked');
break;
default:
$el.val(val);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment