Created
May 27, 2016 09:06
-
-
Save Jehong-Ahn/2c16365c1527bef1523b7a08ec33a056 to your computer and use it in GitHub Desktop.
jQuery form loader
This file contains 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 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