I have an array and it has some data in it. This array contains id, firstname, lastname, email, phone. I also have five form input elements. Now I want to add data in array in these elements. For example, ID should be inserted in ID form field, name should be inserted in name input element etc. how can I achieve this?
here is my code
insert_data_in_form: function(data) {
var form = $("#dataform");
var form_arr = new Array();
form.each(function(i) {
var form_el = form.children("input[type='textfield']");
form_arr.push(form_el);
console.log(data.length);
//for(var j = 0; j < 5; j++) {
form_el.val(data[i]);
//}
});
}
If the values in data array are in the same order as they are in the form then you can do this
Demo: http://jsfiddle.net/joycse06/AJhEW/
UPDATE (Alternate Way):
You can also achieve this using
Implicit Loopingwith this.val(function(index,val)variant of the.val()function like thisDemo: http://jsfiddle.net/joycse06/AJhEW/2/