Skip to content

Instantly share code, notes, and snippets.

@Jehong-Ahn
Created May 27, 2016 09:06
Show Gist options
  • Save Jehong-Ahn/2c16365c1527bef1523b7a08ec33a056 to your computer and use it in GitHub Desktop.
Save Jehong-Ahn/2c16365c1527bef1523b7a08ec33a056 to your computer and use it in GitHub Desktop.
jQuery form loader
function formLoad(data){
$.each(data, function(key, val){
var $el = $("[name="+key+"]:first");
var tagName = $el.prop("tagName");
var type = $el.attr("type");
//console.log($el, tagName, type);
if ( tagName=="INPUT" ) {
if (type=="checkbox" || type=="radio") {
if (val===true) {
$el.prop("checked", true);
}
else if (val===false){
}
else {
$("[name="+key+"][value="+val+"]").prop("checked", true);
}
}
else {
$el.val(val);
}
}
else if ( tagName=="SELECT" ) {
$("[name="+key+"] [value="+val+"]").prop("selected", true);
}
else if ( tagName=="TEXTAREA" ) {
$el.text(val);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment