Loop through the data, not the inputs
so like:
$.each(data, function(k, v) {
var txt = document.getElementById(k);
if (txt) {
$(this).val(v);
}
else {
// could not find <input>
}
});
On Jan 14, 3:35 pm, "T.J. Simmons" <[email protected]> wrote:
> Hi all,
>
> Got a quick question for you. I have a form with several text fields.
> I'm using AJAX to load data into the fields at page load, using JSON.
> Instead of manually writing code to fill each field, I'm wanting to
> use some sort of loop to do this.. but I'm unable to think of a way to
> do so right now.
>
> Here's an example of what I'm thinking of.. The field in the JSON
> object that holds the value corresponds with the field's ID.. ie,
> { "custName": "bob" } can be retrieved by data.custName manually.
>
> success: function(data) {
> $("input:text").each(function(){
> part = $(this).attr("id");
> $(this).val(data.part);
> });
>
> }
>
> but that obviously doesn't work, since it's trying to retrieve "part"
> from the data object, instead of using the variable's value. Does
> anyone have a way of doing this?
>
> Thanks.
>
> -- T.J.