1

I've received a JSON object from my server and I want it to loop through all the returned values and populate two input text fields, which are created dynamically.

At the moment I have 2 records returned but it only populates one set of fields and the other set although created remains unpopulated. The console.log output shows there are two records:

0 Object {emailAdd: "[email protected]", emailDesc: "work"} 
1 Object {emailAdd: "[email protected]", emailDesc: "home"}

This is the Jquery:

var emails = {% raw json_encode(emails) %};
$.each(emails, function(k,v) {
    x = 1;
    z = x++;
    $('<input type="email" placeholder="Email" value="" id="email'+x+'" data-name="emailAdd" /><input type="text" placeholder="Description" value="" id="email'+z+'" data-name="emailDesc"/>').appendTo('#email');
    $('#email' + x + '').val(v.emailAdd);
    $('#email' + z + '').val(v.emailDesc);
    x++;
    console.log(k, v); 
});

It seems to be just returning the last record, instead of all records. How can I show all the returned records?

3
  • You don't have any JSON there. The context you are using it in means the output of your server side template language is a JavaScript object literal. Commented Sep 1, 2014 at 12:32
  • Don't know if this is causing the issue but you are setting the id of the div always just to "email"... id's need to be unique... Commented Sep 1, 2014 at 12:37
  • @nothing9, he is appending "x" and "z" in "email string", so I think this is not the issue. Commented Sep 1, 2014 at 12:41

3 Answers 3

4

You are initializing var x on every each iteration. You could try this:

var emails = {% raw json_encode(emails) %};
x=1;
$.each(emails, function(k,v) {
z=x++;
$('<div class="form-group1" name ="email" id="email"><input type="email" class="form-control" placeholder="Email" value="" id="email'+x+'" data-name="emailAdd" /><br><input type="text" class="form-control" placeholder="Description" value="" id="email'+z+'" data-name="emailDesc"/></div>').appendTo('#email');
$('#email'+x+'').val(v.emailAdd);
$('#email'+z+'').val(v.emailDesc);
x++;
console.log(k,v); 
});

Demo fiddle : http://jsfiddle.net/orooct3q/

Sign up to request clarification or add additional context in comments.

1 Comment

@Ipg Thanks for the spot. I was reinitializing the value back to one.
1

Try this

emails = JSON.parse('{% raw json_encode(emails) %}'); // or use $.parseJSON()

$.each(emails, function(k,v) {
    // code goes here
    console.log(k,v); 
});

1 Comment

There's no need to call JSON.parse. JSON can be used as a Javascript literal.
1

Your code overwrites the previous values since you start counting x=1 in each loop. The following should show you all the data:

var emails = {% raw json_encode(emails) %};
$.each(emails, function(k,v) {
    $('<div class="form-group1" name ="email" id="email"><input type="email" class="form-control" placeholder="Email" value="" id="email' + k + '-0" data-name="emailAdd" /><br><input type="text" class="form-control" placeholder="Description" value="" id="email' + z + '-1" data-name="emailDesc"/></div>').appendTo('#email');
    $('#email'+k+'-0').val(v.emailAdd);
    $('#email'+k+'-1').val(v.emailDesc);
    console.log(k,v); 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.