Assuming I have something like the following:
var address = {id: 100, name: "Gregg", addressOne: "111 1st Street"};
And an HTML form:
<input id="name" />
<input id="addressOne" />
I want to know if there is a way to iterate over all the INPUT elements of the form, and set their values based on the JSON object’s properties. The following is the long approach I could take:
$.each($("input"), function(idx, input) {
if (input.attr("id") == "name") input.val( address.name );
if (input.attr("id") == "addressOne") input.val( address.addressOne );
});
I want to know if there is a way to do the above without the IF statements. Is there some way in JavaScript to dynamically map the two together. I hope this makes sense.
You can use
valmethod:http://jsfiddle.net/GKUMk/