Skip to content

Instantly share code, notes, and snippets.

@elranu
Created December 23, 2013 23:09
Show Gist options
  • Save elranu/8106375 to your computer and use it in GitHub Desktop.
Save elranu/8106375 to your computer and use it in GitHub Desktop.
json to html Form simple script
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="json to html form" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<form id="modelForm" novalidate >
</form>
</body>
</html>
//Json to html form
//replace the var model with your json
var model ={name: "pepe", email : "[email protected]", age: "56"};
String.prototype.format = function() {
var formatted = this;
for( var arg in arguments ) {
formatted = formatted.replace("{" + arg + "}", arguments[arg]);
}
return formatted;
};
var htmlfieldset = '<fieldset><label for="field">{0} : </label><input ng-model="model.account.{1}" type="text"></fieldset>';
var fields = [];
for(var fieldname in model ){
fields.push({ name: fieldname , value: model[fieldname] });
}
for(var index in fields){
var htmlToIsnert = htmlfieldset.format(fields[index].name, fields[index].name);
$("form#modelForm").append(htmlToIsnert);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment