Skip to content

Instantly share code, notes, and snippets.

@slav123
Created September 15, 2013 11:46
Show Gist options
  • Save slav123/6570034 to your computer and use it in GitHub Desktop.
Save slav123/6570034 to your computer and use it in GitHub Desktop.
populate form with json object via JavaScript
function populate_form(data, form) {
$.each( data, function(name, value) {
// grab input
var input = $(":input[name='" + name + "']:not(:button,:reset,:submit,:image)", form );
input.val( ( !$.isArray( value ) && ( input.is(':checkbox') || input.is(':radio') ) ) ? [ value ] : value );
// populate select value
var select = $(":input[type='select']", form );
if (select) {
select.filter(function() {
return $(this).text() == text1;
}).prop('selected', true);
}
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment