Last active
August 29, 2015 13:57
-
-
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.
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
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