1

i just wanted to ask if jQuery provide such function that can supply value or populate all data from object to form's input.

Lets say i have form below :

<form id="frm">
    <input type="text" name="field1"/>
    <input type="text" name="field2"/>
    <input type="text" name="field3"/>
    <input type="text" name="field4"/>
    <input type="text" name="field5"/>
    <input type="text" name="field6"/>
    .... soon
</form>

Then i have object from server side

$.ajax({
    success : function(response){
        $("#frm").supply(response.data);  // Something like that
    }
});

Currently, i am using the function below to make it work.

$.fn.supply = function(data){
    return this.each(function(){
        var element = $(this);
        $.each(data,function(k,v){
            element.find('[name="'+k+'"]').val(v)
        })

    });
};

Is there a function like that on jQuery? i don't know what keyword should i use. Can't find anything on Google.

Romnick Susa
  • 1,279
  • 13
  • 31
  • 2
    There is no one native function like `supply()` to do what you need. The second example is what you need to use. There may be a plugin that will do this, but the behaviour will be exactly the same as what you have, just extracted out – Rory McCrossan Jul 20 '17 at 06:56
  • In case you need to cater for forms with radio buttons or checkboxes, check out this [SO item](https://stackoverflow.com/questions/9807426/use-jquery-to-re-populate-form-with-json-data). You can also try this [jQuery Populate Plugin](https://github.com/dtuite/jquery.populate) – Frank Fajardo Jul 20 '17 at 07:35

0 Answers0