views:

25

answers:

2

Hi,
I want save multiple individual records for a single model. So my form will have <input> elements with IDs that look like this Author0Title; Author1Title; Author2Title, etc.

I will be getting values for these input's using jQuery.getJSON() method.

I want to assign individual values for these input like these automatically.

document.getElementById('Author0Title').value = respose.data[0].title; 

something like..

for(i=0;i<response.data.length; i++){
    var id = 'Author' + i + 'Title';
    document.getElementById(id).value = respose.data[0].title;
}

But it is not working. I appreciate any help.

Thanks.

+1  A: 

Could it be that you're misspelling respose -> response?

Otherwise, "should work". Assuming your JSON actually matches what you're looking for in this code.

Since you're using jQuery, you might want to use $('#Author' + i + 'Title').val(response.data[i].title); instead - although it does the same.

kander
+3  A: 

If you're using jQuery:

for (var i = 0; i < response.data.length; i++) {
  $('#Author' + i + 'Title').val(response.data[i].title);
}

That's pretty close to your example, except that you've got '0' coded in as the index instead of 'i'.

Make sure that your <input> elements really are using both an "id" and a "name" that's constructed as you expect. If they're just getting the "name" attribute set, you could do this:

  $('input[name=Author' + i + 'Title]').val(response.data[i].title);
Pointy