0

I have JSON values look like this

var results=[obj1,obj2,obj3];

and in

obj1['ádd':'usa','phone':121]
obj2['ádd':'cal','phone':143] 

and so. on.

Here I want to print all obj's address and pass to HTML id element.

I have done this way but it is print the only the first value but in console printing all values.

for (var i = 0; i < results.length; i++) {
  console.log(results[i].add);
  var jjj=(results[i].add);

  document.getElementById('target_2').innerHTML=jjj;
}

How to solve this problem?

4
  • 2
    I don't understand what ['ádd':'usa','phone':121] obj2['ádd':'cal','phone':143] is supposed to mean. This is not valid JS. Did you mean {'ádd':'usa','phone':121}? Commented Nov 18, 2016 at 2:13
  • 1
    Looks like you need to understand Arrays and Object Literals. Commented Nov 18, 2016 at 2:14
  • Can you please give full json code here? Commented Nov 18, 2016 at 2:18
  • What you show is not JSON, it is a JavaScript Object/Array literal/initializer. Commented Nov 18, 2016 at 2:19

2 Answers 2

4

You are consistently replacing the innerHTML of target_2 rather than appending to it. You want to do something like this instead:

document.getElementById('target_2').innerHTML+=jjj;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I forgot to append it. it's work like charm.
2
var jjj = '';
for (var i = 0; i < results.length; i++) {
   console.log(results[i].add);
   jjj += results[i].add;
}
document.getElementById('target_2').innerHTML = jjj;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.