Search code examples
jqueryjsonformsloadget

jQuery how to load some json records into a form fields?


i have a json file:

{
   "data":"Click",
   "size":"Here"
}

and a form:

<form>
   First name: <input type="text" name="firstname" /><br />
   Last name: <input type="text" name="lastname" />
</form>

i was wondering what is the correct sintax for loading multiple json records into multiple form elements?? Something like a form that u want to u want to modify and the values get queered into those fields

i know i can use:

$getJSON('link_to_json_file' function() {

});

but i'm stuck here thanks


Solution

  • Or if your data return was fieldname value pairs like:

    {"firstname" : "John", "lastname" : "Doe"}
    

    you could do it like:

    $.getJSON('url_to_file', function(data) {
        for (var i in data) {
            $('input[name="'+i+'"]').val(data[i]);
        }
    });