-2

If I need to see my out put in the page not in console.log i want use document get Element By Id how can i do that? he gave me just one result, Steve only !!

[Code: ] https://i.stack.imgur.com/ISqzT.png

<script>
    var friends = {};
    friends.bill = {
      firstName: "Bill",
      lastName: "Gates",
      number: "(206) 555-5555",
      address: ['Microsoft Way']
      };
    friends.steve = {
      firstName: "Steve",
      lastName: "Jobes",
      number: "(444) 111 000",
      address: ["Apple way"]
    };
    var list = function(obj) {
      for( var key in obj){
        console.log(obj);
      document.getElementById("demo").innerHTML = key + "<br>";
      }
    }

    var search = function(name) {
      for(var key in friends){
        if(name === friends[key].firstName){
          console.log(friends[key]);
        }
      }
    }
    list(friends);
    // search("Steve");
  </script>

2 Answers2

1

Hey the only change you have to make is using += instead of just = for the innerHTML function. Here you go:

text.innerHTML+=friend + "
";

Paul G
  • 52
  • 3
  • in the same example if i'm doing like that? var search = function(name) { for(var key in friends){ if(name === friends[key].firstName){ console.log(friends[key]); document.getElementById('search').innerHTML += friends[key] + "
    "; } } } output will be [object Object] how can i fix that??
    – Mohamed Ahmed May 20 '16 at 06:49
0

Simply append the result to your innerHTML, do not overwrite it, likewise: document.getElementById("demo").innerHTML += key + "<br>";